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.
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 8HintThe 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;
{
int v;
node* pnext;
}*head;
int n;
void init()
{
int val, i;
node* pnod;
node* tmp = NULL;
head = NULL;
{
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, 如果输入符合要求, 可以去除此处理
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;
}
}
{
if (tmp->v%2 != 0)
++tmp->v;
tmp = tmp->pnext;
if (tmp == head)
break;
}
}
bool isFinish(node* head)
{
node* pnodtmp = head->pnext;
{
node* pnodtmp = head->pnext;
while (pnodtmp != head)
{
if (pnodtmp->v != head->v)
return false;
{
if (pnodtmp->v != head->v)
return false;
pnodtmp = pnodtmp->pnext;
}
return true;
}
}
return true;
}
void doOneRound(node* head) // 返回true: finish退出
{
node* pnodtmp = head->pnext;
int vadd = head->v/2, val;
{
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
{
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;
}
}
{
if (pnodtmp->v%2 != 0)
++pnodtmp->v;
pnodtmp = pnodtmp->pnext;
if (pnodtmp == head)
break;
}
}
void release(node* head)
{
node* pnodtmp = head->pnext;
node* pnode;
{
node* pnodtmp = head->pnext;
node* pnode;
while (pnodtmp != head)
{
pnode = pnodtmp;
pnodtmp = pnodtmp->pnext;
delete pnode;
}
delete head;
head = NULL;
}
{
pnode = pnodtmp;
pnodtmp = pnodtmp->pnext;
delete pnode;
}
delete head;
head = NULL;
}
void solve()
{
int step = 0;
{
int step = 0;
init();
while (true)
{
if (isFinish(head))
{
cout<<step<<" "<<head->v<<endl;
break;
}
doOneRound(head);
++step;
}
release(head);
}
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;
}
{
while (cin>>n)
{
if (n == 0)
break;
solve();
}
return 1;
}
- 1052. Candy Sharing Game
- Candy Sharing Game
- 1034:Candy Sharing Game
- 1034 Candy Sharing Game
- 1052. Candy Sharing Game
- hdu1034-Candy Sharing Game
- hdu1034 Candy Sharing Game
- Candy Sharing Game
- POJ1666(Candy Sharing Game)
- sicily1052 Candy Sharing Game
- Sicily: Candy Sharing Game
- Candy Sharing Game
- Candy Sharing Game
- HDOJ Candy Sharing Game
- HDU1034 Candy Sharing Game
- HDU1034 Candy Sharing Game
- hdu 1034 Candy Sharing Game
- POJ-1666:Candy Sharing Game
- 在vs2005中配置directshow开发环境(转)
- 依赖关系属性
- php调用c++程序例子
- JAVA小程序求助
- 冲击极速(图)
- Candy Sharing Game
- PyDev for Eclipse 简介
- 终于在csdn上安家了.开心~~~
- JAVA小程序求助
- 技术成就梦想_Java-SSH集成
- oracle体系结构2
- JAVA小程序求助
- apache模块 多线程编程
- JAVA小程序求助