题目:
Description
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 students, 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 Notes: The game ends in a finite number of steps because: 1. The maximum candy count can never increase. 2. The minimum candy count can never decrease. 3. No one with more than the minimum amount will ever decrease to the minimum. 4. If the maximum and minimum candy count are not the same, at least one student with the m
程序实现:
#include <iostream>
using namespace std;
struct Node {
// members
int value, add; // value is the current num, add is the adding num
Node *prev;
// constructor
Node();
Node(int value_, Node *add_on = NULL);
};
Node::Node() {
value = 0;
add = 0;
prev = NULL;
}
Node::Node(int value_, Node *add_on) {
value = value_;
prev = add_on;
}
bool same_num(Node *front, int n) {
//cout << "Check candy num" << endl;
Node* stu = front;
for (int i = 0; i < n; i++) {
if (stu->value != front->value) {
return false;
}
stu = stu->prev;
}
return true;
}
int main () {
int n, candy; // n is the num of stu, candy is the num each stu has
Node *prev_stu, *front_stu, *new_stu;
// prev_stu:the prev stu, front:front stu, new_stu: currently adding stu
int count;
while (cin >> n && n) {
prev_stu = NULL;
count = 0;
// initialize the game
for (int i = 0; i < n; i++) {
cin >> candy;
new_stu = new Node(candy, prev_stu);
if (!i) { // the first stu
front_stu = new_stu;
}
prev_stu = new_stu;
}
front_stu->prev = new_stu;
//cout << "Adding stu completed" << endl;
// start the game
while (!same_num(front_stu, n)) {
new_stu = front_stu;
count++;
for (int i = 0; i < n; i++) {
new_stu->prev->add = (new_stu->value) / 2; // give prev stu half
new_stu->value /= 2; // delete candy num;
new_stu = new_stu->prev;
}
//cout << "Round: " << count << " giving completed" << endl;
new_stu = front_stu;
for (int i = 0; i < n; i++) {
new_stu->value += new_stu->add; // renew candy num, add extra
new_stu->add = 0;
if (new_stu->value % 2) {
new_stu->value++;
}
new_stu = new_stu->prev;
}
//cout << "Round: " << count << " renew completed" << endl;
}
cout << count << " " << front_stu->value << endl;
}
return 0;
}
解题感想:
因为是最近学到的链表内容相关,因此写博客文章来做学习记录。