Candy Sharing Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4448    Accepted Submission(s):
2718

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
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
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.

 
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#define MAX 100010
#define LL long long
#define INF 0xfffffff
#define fab(x) (x)>0?(x):(-x)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,j,i,t;
int s[MAX],s1[MAX];
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
{
scanf("%d",&s[i]);
s1[i]=s[i];
}
int flag=1;
int ans=0;
int ant=0;
while(flag)
{
ans++; s[1]=s1[n]/2+s1[1]/2;
if(s[1]&1)
s[1]+=1; //s1[1]=s[1]; for(i=2;i<=n;i++)
{
s[i]=s1[i-1]/2+s1[i]/2;
if(s[i]&1)
s[i]+=1;
//s1[i]=s[i];
}
for(i=1;i<=n;i++)
s1[i]=s[i];
// for(i=1;i<=n;i++)
// {
// printf("%d# ",s[i]);
// }
// printf("\n"); int k=0;
for(i=1;i<n;i++)
{
if(s[i]==s[i+1])
k++;
else
break;
}
for(i=1;i<=n;i++)
printf("%d ",s[i]);
printf("\n");
if(s[1]==s[n])
k++;
if(k==n)
{
flag=0;
ant=s[1];
}
}
printf("%d %d\n",ans,ant);
}
return 0;
}

  

hdu 1034 Candy Sharing Game的更多相关文章

  1. HDU 1034 Candy Sharing Game (模拟)

    题目链接 Problem Description A number of students sit in a circle facing their teacher in the center. Ea ...

  2. POJ - 1666 Candy Sharing Game

    这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...

  3. Candy Sharing Game(模拟搜索)

    Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. Hdu 4465 Candy (快速排列组合+概率)

    题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...

  5. HDU 5291 Candy Distribution DP 差分 前缀和优化

    Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...

  6. HDU 4780 Candy Factory

    Candy Factory Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

  7. hdu 4465 Candy(二次项概率)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4465 参考博客:http://www.cnblogs.com/goagain/archive/2012 ...

  8. M - Candy Sharing Game

    Description A number of students sit in a circle facing their teacher in the center. Each student in ...

  9. Candy Sharing Game(hdoj1034)

    Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...

随机推荐

  1. spring结合时,web.xml的配置

    <!-- 1. web.xml配置 <context-param> <param-name>webAppRootKey</param-name> <pa ...

  2. CentOS7.1 JDK安装 和 CentOS7.1配置yum源

    1.卸载自带OPENJDK #查看自身jdk java -verson #查看自身安装的java  rpm -qa | grep java #显示如下 python-javapackages-3.4. ...

  3. A9系统时钟用外部

     问个笨蛋的问题,,电脑主板的主频是由外部时钟倍频得来,还是内部时钟倍频?? [ARM11]瘋子 2015/5/5 19:08:16 @蓝凌风 [x86]蓝凌 2015/5/5 19:08:25 外部 ...

  4. C#如何使用和开发自定义配置节

    在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...

  5. UNICODE,GBK,UTF-8区别

    简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...

  6. 函数fsp_alloc_seg_inode

    从inode page中申请inode entry inode = fsp_alloc_seg_inode(space_header, mtr); /************************* ...

  7. poj3692

    首先这道题很容易想到二分图相关(给的很明确了): 但是我们发现,男孩之间都互相认识,女孩之间也互相认识 这样是不能划分点集的 但是男孩之间都互相认识,女孩之间也互相认识,所以男孩和男孩,女孩和女孩之间 ...

  8. bzoj1856

    这是一道无比涨姿势的题目 首先总结一下这种输入几个数的题目, 一般不是递推就是数学题 显然,这道题用递推是无法做到O(n)的复杂度的 那我们就考虑这是一道数学题了 我已开始纠结在正向思维了,正向求好像 ...

  9. bzoj2818gcd

    原理很简单 题解我就不自己写了…… 做这题的时候,懂得了一个非常重要的转化:求(x, y) = k, 1 <= x, y <= n的对数等于求(x, y) = 1, 1 <= x, ...

  10. 陈正冲老师讲c语言void关键字

    1. void a void的字面意思是“空类型”,void *则为“空类型指针”,void *可以指向任何类型的数据. void几乎只有“注释”和限制程序的作用,因为从来没有人会定义一个void变量 ...