candy sharing

I been working on candy sharing problem


A number of students sit in a circle facing
their teacher in the center. Each student
initially has an even number of pieces of
candy. When the teacher blows a whistle, each
student simultaneously gives half of his or
her candy to the neighbor on the right. Any
student, who ends up with an odd number of
pieces of candy, is given another piece by
the teacher. The game ends when all students
have the same number of pieces of candy.

Write a program which determines the number of
times the teacher blows the whistle and the final
number of pieces of candy for each student from
the amount of candy each child starts with.

Input

The input may describe more than one game. For
each game, the input begins with the number N
of stude nts, followed by N (even) candy counts
for the children counter-clockwise around the
circle. The input ends with a student count of
0. Each input number is on a line by itself.

Output

For each game, output the number of rounds of the
game followed by the amount of candy each child ends
up with, both on one line.

Sample Input

6
36
2
2
2
2
2
11
22
20
18
16
14
12
10
8
6
4
2
4
2
4
6
8
0
Sample Output

15 14
17 22
4 8




I have been used temp vector because every number should move at the same time
but time take too long.( time exceeded)

any one have better idea to solve this problem?

my code followed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

#include <iostream>
#include <string>
#include <vector>
using namespace std;


void clean(vector<int> &vec){
    
    while(vec.size() !=0){
        
        vec.pop_back();
    }
    
}

bool checkAllEqual(vector<int> &vec){
    
    
    bool result;
    for (size_t i=0 ; i < vec.size(); i++) {
        if( i < vec.size()-1){     
           //cout << vec[i] << " and " << vec[i+1]<< endl;
           result =  (vec[i] == vec[i+1]);
        if(result== false) return false;
        else {
            //cout << "true" <<endl;
            result = true;
            }
        }
        //return result;
    }
    //cout <<"returning..." <<endl;
    return result;
    
}


void print(vector<int> vec){
    for(size_t i=0 ; i < vec.size() ; i++){
        
        cout << vec[i] <<" ";
    }
    cout <<endl;
    
}




int round(vector<int>& vec){
    
    int count =0;
    vector<int> temp;
    
    
   // cout << "size : " << vec.size() <<endl;
    while( checkAllEqual(vec)!=true){
        //print(vec); 
        if( temp.size() !=0) clean(temp);
        if( temp.size() ==0){
            for(size_t i=0 ; i <vec.size() ; i++) temp.push_back(0);
        }
        for (size_t i=0 ; i < temp.size(); i++) {
            //cout <<" one" <<endl;
            
            if( i==0){
                // cout << " two " <<endl;
                if(vec[vec.size()-1] %2 ==1){
                  //  cout << " here " << vec[vec.size()-1] <<" and "<< vec[vec.size()-1]+1  <<endl;
                    vec[vec.size()-1] = vec[vec.size()-1]+1;

                }
                
                temp[i] = vec[vec.size()-1]/2;
            }else if(i !=0){
                if(vec[i-1] %2 ==1) vec[i-1] = vec[i-1]+1;
                temp[i] = vec[i-1] /2;
            }
        }
       // cout << "temp "; print(temp);
        //print(temp);
        for (size_t i=0 ; i < vec.size() ;i++){
            //cout << " three " <<endl;
            vec[i] = vec[i]/2;
            //print(vec);
            
        }
       // cout <<"vector 1 : ";
        //print(vec);
        //return 0;
        for (size_t i=0 ; i < vec.size() ;i++){
            //cout << " four" <<endl;
            vec[i] = vec[i] + temp[i];            
        }
       
        
        //clean(temp);
        count++;
      // cout << "vector final "; print(vec);
               
    }
    return count-1;
}


int main (int argc, const char * argv[])
{
    
    int student;
    int candy;
    vector<int> classes;
    while(true){
        
        cin>> student;
        if( classes.size() != 0) clean(classes);
        if(student > 1){
        if(student ==0 ) return 0;
        for( int i=0 ; i < student
            ; i++){
            cin>> candy;
            classes.push_back(candy);
            
        }
        //cout <<"size : " <<classes.size() <<endl;
        if( classes.size() >1){
        cout << round(classes) <<" "<<classes[0] << endl;
      
        //cout << checkAllEqual(classes)<<endl;
       // if(  checkAllEqual(classes)==true ) cout << "true" <<endl;
       // else {cout << "false" <<endl;}
        
        
            }
        }

   
    }
}
Topic archived. No new replies allowed.