模拟:HDU1034-Candy Sharing Game
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用于记录每次分一半的数目;
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的更多相关文章
- HDU-1034 Candy Sharing Game 模拟问题(水题)
题目链接:https://cn.vjudge.net/problem/HDU-1034 水题 代码 #include <cstdio> #include <algorithm> ...
- HDU1034 Candy Sharing Game
Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...
- Candy Sharing Game(模拟搜索)
Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ - 1666 Candy Sharing Game
这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...
- hdu 1034 Candy Sharing Game
Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1034 Candy Sharing Game (模拟)
题目链接 Problem Description A number of students sit in a circle facing their teacher in the center. Ea ...
- 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:248 解决:194 题目描述: A number of students sit in a circle facing their teac ...
- M - Candy Sharing Game
Description A number of students sit in a circle facing their teacher in the center. Each student in ...
- Candy Sharing Game(hdoj1034)
Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...
随机推荐
- android 开发DatePickerDialog/TimePickerDialog对话框的实现
AndroidAPI提供了Dialog对话框控件,DatePickerDialog/TimePickerDialog均是AlertDialog的子类,通过DatePickerDialog/TimePi ...
- Java 多线程的实现方法
package com.jckb; /**多线程实现的两种方法 * * @author gx * */ public class Test2 { public static void main(Str ...
- input累加赋值
需求如下:第一个input添加字符到第二个input,第二个input需要累加. html: <tr> <td rowspan="2" class="D ...
- Quartz 定时器,同时运用多个定时器
效果:每天执行两个定时器,两个定时器不相关联.jar版本Quartz 2.2.3 Java工程结构图 jar 包下载: 链接: https://pan.baidu.com/s/1-7dh620k9P ...
- 前端js优化方案(一)
最近在读<高性能javascript>,在这里记录一下读后的一些感受,顺便加上自己的一些理解,如果有兴趣的话可以关注的我的博客http://www.bloggeng.com/,我会不定期发 ...
- CST,CET,UTC,GMT,DST,Unix时间戳几种常见时间概述与关系(转)
转自:http://www.cnblogs.com/frontendBY/p/5215785.html 1.UTC: Universal Time Coordinated 协调世界时,又称世界标准时间 ...
- SHOW SLAVE STATUS 详解
MySQL同步功能由3个线程(master上1个,slave上2个)来实现.执行 DE>START SLAVEDE> 语句后,slave就创建一个I/O线程.I/O线程连接到master上 ...
- coursera_ML_1
机器学习定义: A computer program is said to leran from experience E with respect to some task T and some ...
- Android(java)学习笔记74:ListViewProject案例(ListView + ArrayAdapter)
1. 首先是MainActivity.java文件,如下: package com.himi.lv1; import java.util.ArrayList; import java.util.Lis ...
- vuejs组件的重要选项
new Vue({ el:'#demo', data:{ message:'Hello vue.js!' } }) 我们看到这个括号里面包含了很多中间的选项,小括号里面其实是一些参数,这些参数指定了实 ...