Waterloo Cup-The realization of the Candy C language of the previous questions

Source: Internet
Author: User
Tags time limit
All previous questions are divided into sweetsTime limit: 1.0s memory limit: 256.0MB

Problem description
There are N children sitting in a circle. The teacher randomly sends a couple of sweets to each child and then plays the following game:
Each child treats his or her sweets in half to the children on the left hand side.
After a round of sugar, the children with odd sugars were supplied 1 sweets by the teacher, which became even.
Repeat the game until all the children have the same number of sweets.
Your task is to predict how many sweets the teacher will need to reissue in the event of a known initial candy.
Input format
The program first reads an integer N (2<n<100), indicating the number of children.
followed by a row of n-even numbers separated by a space (every even number is no greater than 1000, not less than 2)
Output format
Requires the program to output an integer, indicating the number of sweets the teacher needs to reissue.
Sample input
3
2 2 4
Sample output
4

Analysis: This problem data is small (in fact Waterloo Cup test data is 2<n<1000, Waterloo Cup test data are unreliable), directly with the array and linked list are full marks, recommended by the linked list familiar.
Code here: (linked list)
#include <stdio.h> #include <stdlib.h> typedef struct node{int now;
struct Node*next;

}student;
	int main () {int n, x=0, y, K, s=0, o[100], m=0;
	int I, J;
	
	Student *head, *r, *p, *q;
	
	scanf ("%d", &n);
	p = (student*) malloc (sizeof (student));
	head = p;
	P->next = head->next;
	r = P;
		for (i = 0; i < n; i + +) {scanf ("%d", &k);
		p = (student*) malloc (sizeof (student));
		P->now = k;
		P->next = head->next;
		R->next = p;
	r = r->next;
		while (1) {p = head->next;
			for (i = 0; i < n; i + +, p = p->next) {q = p->next;
	            if (P->now = = Q->now) m + +;
				if (M = = N) {printf ("%d\n", s);
				return 0;
		} m = 0;
			for (i = 0;i < n; i + +) {y = p->now/2;
			P->now = y;
			P->now = P->now + x;
			x = y;
		p = p->next;
		} P->now = p->now+x;					x = 0;
				The number of global variables with loops does not affect the next round of loops for (i = 0; i < n; i + +) {if (p->now% 2!= 0) {(p->now) + +;
			s + +; }
			p = p->next; }
	}
}

code here (Array)
#include <stdio.h>  

#define SIZE 1000+10

int main () {  
    int a[size];  
    int n;  
    int s = 0;  
    int i;  
      
    scanf ("%d", &n);  
      
    for (i = 0; i < n; i + +)  
        scanf ("%d", &a[i]);  
  
    while (1) {  
        int k = 0;  
        for (i = 0; i < n; i + +) {  
            if (A[i]% 2 = 1) {  
                a[i] + +;  
                s + +;  
            }  
            if (a[i] = = A[0])  
                k + +;  
        }  
        if (k = = n) break  
            ;            
          
        int temp = a[n-1]/2;  
        int t;  
        for (i = 0; i < n; i + +) {  
            t = a[i]/2;  
            A[i] = t + temp;  
            temp = t;  
        }  
    }  
      
    printf ("%d", s);
    return 0;  
}  





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.