Stream processing characteristics of a computer

Source: Internet
Author: User

Problem description

There are N children sitting around in a circle. The teacher gave each child a random number of candies and then played the following games:

Each of the children gave their sweets half to the children on the left hand side.

After a round of sugar, the children with odd sugars were supplied with 1 sweets by the teacher and turned into an even number.

Repeat the game until all the children have the same number of candies.

Your task is to predict how many candies a teacher will need to reissue in a known initial candy situation.

Input format

The program first reads an integer N (2<n<100), which indicates the number of children.
Next is a line of n even numbers separated by a space (each even number is not greater than 1000, not less than 2)

Output format

Ask the program to output an integer that indicates the number of sweets the teacher needs to reissue.

Sample input

3
2 2 4

Sample output

4

  first, the real-world model can be simplified . The simplification of the candy model, assuming that everyone at the same time the candy into two parts, and then half the left hand, and then receive part of the right hand. In addition to the last person, everyone else's candy is equal to the average value of the candy on the right side (note that the old value must be used). The last person's candy equals the mean value of the first person's candy (note that you must use the old value). As follows

This piece of code is the most critical, though simple but error prone. The reason is not to recognize the computer's flow processing characteristics , what is the computer's flow processing characteristics, in fact, think of a machine to know that the Turing machine is a character stream, the computer is the same, its processing can only flow, such as the tree traversal, graph traversal, because the tree and graph is not a linear flow, So can not be directly processed, only the first to traverse into a linear stream to be processed by the computer. This stream processing characteristics of the computer conflict with the real world model, a lot of things in the real world are happening at the same time, the computer can not be very good simulation, such as the problem of the ant cold, the real world Ant State is updated at the same time, if the computer processing, then only with the new part of the Ant State, This is obviously going to go wrong. In the case of the children's game process is instantaneous at the same time, then the computer in the simulation should be careful, must pay attention to update the order of the data. My approach is to first update the first person, he used the data is his original candy number and the right child's original candy number mean, and then deal with the second person, he used the data are old values, and so on to the penultimate person. The last person to deal with alone, he uses the mean value of his and the first person's old values, but this time the first person's value has been updated, so in advance to make a copy of the first person's old value. The specific implementation code is as follows:

#include <stdio.h>intCheckintA[],intN) {     for(intI=0; i<n-1; i++)    if(a[i]!=a[i+1])return 0; return 1;} Main () {intN;  while(~SCANF ("%d",&N)) {inta[ -]; intCount=0;  for(intI=0; i<n;i++) scanf ("%d",&A[i]);  while(!Check (a,n)) {            intt=a[0];  for(intI=0; i<n-1; i++) A[i]= (a[i]+a[i+1])/2; A[n-1]= (a[n-1]+T)/2;//be careful with the new order, be sure to use data that has not been followed             for(intI=0; i<n;i++)            if(a[i]%2) {A[i]++; Count++; }} printf ("%d\n", Count); }} 

Stream processing characteristics of a computer

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.