解题心得:

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. ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  2. node官方docker镜像运行bower 提示 permission denied 解决方法

    在使用node官方docker镜像部署node应用时,应用需要npm的scripts中运行bower install 来安装前端包,但是用docker 构建时失败,提示 permission dein ...

  3. linux cached过高导致性能变低

    场景: 拿到了客户50个文件,平均每个文件大概40M左右的txt,文件在S3上,需要导入到数据库,40M解析出来大概是80W条左右的数据. 描述: 在刚开始执行导入时,因为数据验证复杂程度不同,每个文 ...

  4. Servlet高级部分Filter(过滤器)

    一:Filter称之为"过滤器",用在Servlet外,对request和response进行修改.它是AOP(面向切面编程思想的一种体现),Filter中有一个FilterCha ...

  5. ArcSDE空间数据库中SDE用户使用探讨 (转载)

    ArcSDE作为空间数据库解决方案,应用非常广泛,本短文将尝试描述SDE的工作机制,简要说明空间数据库中SDE用户的使用方法.ArcSDE如何工作ArcSDE属于中间件技术,其本身并不能够存储空间数据 ...

  6. v-if与v-show的区别

    一.区别 v-if 动态的向DOM树内添加或者删除DOM元素:“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建:在初始渲染条件为假时,什么也不做. v-sho ...

  7. (转) HTTP Request header

    HTTP Request header 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基 ...

  8. linux命令 ——目录

    开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令.学习的主要参考资料为: 1.<鸟哥的linux私房菜> 2.http://codingstan ...

  9. IOS 创建一个可以随意拉伸不变形的图片

    创建一个扩展 UIImage的类 #import "UIImage_Extension.h" @implementation UIImage+Extension /** *返回一张 ...

  10. OpenLayers学习笔记2——坐标转换问题

    参照别人的添加marker的demo来改造时,发现无论怎样更改经纬度,都是停留在同一个位置.过了一两天突然想起可能是坐标参考的问题,尝试搜了一下,果然是这个问题.问题是这样子的: WMTS中地图的坐标 ...