解题心得:

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. Unity GL 画圆

    Unity下GL没有画圆的函数,只能自己来了. 如果能帮到大家,我也很高兴. 虽然没有画圆的函数,但是能画直线,利用这一点,配合微积分什么的,就可以画出来了.反正就是花很多连在一起的直线,每条直线足够 ...

  2. 《C#高效编程》读书笔记04-使用Conditional特性而不是#if条件编译

    #if/#endif语句常用来基于同一份源代码生成不同的编译结果,其中最常见的就是debug版和release版.但是这在实际应用中并不是非常友好,因为它们容易被滥用,其代码也难以理解或调试. C#为 ...

  3. 关于Mdi窗口-父窗口上的控件把子窗口的挡住的问题

    using System.Runtime.InteropServices; [DllImport("user32")] public static extern int SetPa ...

  4. Struts2 源码分析-----Hello world

    今天第一天学习struts2,没学过怎么办,那当然是helloworld.感觉嘛,学习的基本流程都差不多,就是helloworld,开发环境,然后就是逐个按照知识点打demo,打着打着你就会发现str ...

  5. mysql配置jdbc

    jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=t ...

  6. mui的ajax例子2

    mui.post()方法 前端页面: <!DOCTYPE html><html><head> <meta charset="utf-8"& ...

  7. ansible基本操作

    ansible优点:redhat自带工具,可通过rpm或yum直接安装:客户端免安装:操作通过ssh验证操作:可以通过自定义hosts文件对可操作主机进行分类,方便批量操作 #ansible操作格式, ...

  8. intelij idea相关笔记--持续更新

    一.快捷键: Ctrl+F 文件内查找 Ctrl+Shift+F 全局查找 Ctrl+Shift+N 查找文件 Ctrl+Alt+← 返回上一步 Ctrl+Alt+→ 返回下一步 二.编译相关: 如果 ...

  9. Mysql数据库插入时乱码问题解决

    我们在利用cmd的黑屏界面进行mysql数据的插入时往往会出现不能插入的情况,这个原因是因为我们系统虽然和服务器端即mysql的数据库采用的都是统一的utf8的编码,但是在传输的过程中会变成iso88 ...

  10. hihocoder 1109 堆优化的Prim算法

    题目链接:http://hihocoder.com/problemset/problem/1109 , 最小生成树 + 堆优化(优先队列). 可以用优先队列,也可以自己手动模拟堆,为了练手,我两种都试 ...