Candy Sharing Game

来源:互联网 发布:图片尺寸打印软件 编辑:程序博客网 时间:2024/04/29 08:35
Problem 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
6362222211222018161412108642424680
 

 

Sample Output
15 1417 224 8
Hint
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 minimum amount must have their count increase.
 

 

Source
Greater New York 2003
参考代码(基于循环链表):



#include <iostream>
using namespace std;
struct node
{
 int v;
 node* pnext;
}*head;
int n;
void init()
{
 int val, i;
 node* pnod;
 node* tmp = NULL;
 head = NULL;
 // 创建循环链表
 for (i = 0; i < n; ++i)
 {
  cin>>val;
  pnod = new node;
  pnod->v = val;
  pnod->pnext = NULL;
  if (i == 0)
   head = pnod;
  else
   tmp->pnext = pnod;
  tmp = pnod;
 }
 tmp->pnext = head;
 tmp = head;  // 奇数补1, 如果输入符合要求, 可以去除此处理
 while (true)
 {
  if (tmp->v%2 != 0)
   ++tmp->v;
  tmp = tmp->pnext;
  if (tmp == head)
   break;
 }
}
bool isFinish(node* head)
{
 node* pnodtmp = head->pnext;
 while (pnodtmp != head)
 {
  if (pnodtmp->v != head->v)
   return false;
  pnodtmp = pnodtmp->pnext;
 }
 return true;
}
void doOneRound(node* head)  // 返回true: finish退出
{
 node* pnodtmp = head->pnext;
 int vadd = head->v/2, val;
 while (pnodtmp != head)
 {
  val = pnodtmp->v/2;
  pnodtmp->v = val+vadd;
  vadd = val;
  pnodtmp = pnodtmp->pnext;
 }
 head->v = head->v/2+vadd;
 pnodtmp = head;   // 奇数补1
 while (true)
 {
  if (pnodtmp->v%2 != 0)
   ++pnodtmp->v;
  pnodtmp = pnodtmp->pnext;
  if (pnodtmp == head)
   break;
 }
}
void release(node* head)
{
 node* pnodtmp = head->pnext;
 node* pnode;
 while (pnodtmp != head)
 {
  pnode = pnodtmp;
  pnodtmp = pnodtmp->pnext;
  delete pnode;
 }
 delete head;
 head = NULL;
}
void solve()
{
 int step = 0;
 init();
 while (true)
 {
  if (isFinish(head))
  {
   cout<<step<<" "<<head->v<<endl;
   break;
  }
  doOneRound(head);
  ++step;
 }
 release(head);
}
int main(int argc, char* argv[])
{
 while (cin>>n)
 {
  if (n == 0)
   break;
  solve();
 }
 return 1;
}
原创粉丝点击