解题心得:

1、我是用的模拟的算法直接模拟的每一轮的分配方法的得出的答案,看到有些大神使用的链表做的,好像链表才是这道题的镇长的做法吧。

题目:

Candy Sharing Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6525    Accepted Submission(s): 3962

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<bits/stdc++.h>
using namespace std;
int a[10000],b[10000];//b用于记录a分配的数目;
int main()
{
int n;
bool flag;
while(~scanf("%d",&n))
{
if(n == 0)
break;
int sum = 0;
flag = false;
//初始化输入
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]%2)
a[i]+=1;
}
while(!flag)
{
sum ++;//记录经过了多少轮;
for(int i=1;i<=n;i++)
{
b[i] = a[i]/2;
a[i]/=2;
}
for(int i=2;i<=n;i++)
a[i] = a[i] + b[i-1];
a[1] = a[1] + b[n];
for(int i=1;i<=n;i++)
if(a[i]%2)
a[i]++;
int k;
for(k=1;k<n;k++)
if(a[k] != a[k+1])
break;
if(k == n)
flag = true;//用于标记是否已经分配平衡;
}
printf("%d %d\n",sum++,a[n]);
}
return 0;
}

水题:HDU1034-Candy Sharing Game的更多相关文章

  1. HDU-1034 Candy Sharing Game 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1034 水题 代码 #include <cstdio> #include <algorithm> ...

  2. HDU1034 Candy Sharing Game

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

  3. CF330 C. Purification 认真想后就成水题了

    C. Purification time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  4. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  6. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

  7. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  8. gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,

    1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人 ...

  9. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

随机推荐

  1. SQLServer常见性能问题

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  2. java+elipse安装及部分问题

    1. elipse下载.安装.jdk环境配置教程: https://www.cnblogs.com/ForestDeer/p/6647402.html 2.eclipse使用教程: https://j ...

  3. sql单列合并

    有一组这样的数据 1  a  10 2  b  2 4  c  5 1  a  5 在应用中,我们可能需要把出现a的数据合并显示:  1   a   10,5 sqlite上实现:  SELECT   ...

  4. webpack4流程笔记

    初始化 mkdir webpack-demo   ->新建文件夹  cd webpack-demo  ->进入文件夹 第一步 npm init -y  -> 初始化项目(生成pack ...

  5. addin修改启动路径

  6. Unity3d 游戏中集成Firebase 统计和Admob广告最新中文教程

    之前写过俩相关的教程,最近发现插件官方更新了不少内容,所以也更新一篇Firebase Admob Unity3d插件的教程,希望能帮到大家. Firebase Admob Unity3d插件是一个Un ...

  7. SqlServer Alwayson 搭建排错记录(二)

    下面记录下建立好alwayson可用性组后,向可用性组内添加数据库出现过的问题及解决方法 一.数据库未处于恢复状态 将数据库联接到可用性组的时候报错: 数据库“XXXX”未处于恢复状态,而此状态是镜像 ...

  8. HDU5152 线段树 + 数论

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5152 ,线段树区间更新 + 点更新 + 数论知识(数论是重点QAQ),好题值得一做. BestCode ...

  9. java中list强转为map类型

    起因:读取数据库文件的测试用例,测试用例需要存放到一个map中,方便下次调用, 读取的内容返回的内容存放在一个list中,并且数据内容是key=value的形式,最开始使用切片方式,做了很多无用功,后 ...

  10. POJ-3436 ACM Computer Factory---最大流+拆点

    题目链接: https://vjudge.net/problem/POJ-3436 题目大意: 每台电脑有p个组成部分,有n个工厂加工电脑.每个工厂对于进入工厂的半成品的每个组成部分都有要求,由p个数 ...