九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:248
解决:194
- 题目描述:
-
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.
- 输入:
-
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.
- 输出:
-
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.
- 样例输入:
-
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
- 样例输出:
-
15 14
17 22
4 8
- 提示:
-
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> #define M 1000 int shouldEnd(int *a, int n)
{
if (n < 2)
return 1;
for (int i=1; i<n; i++)
{
if (a[0] != a[i])
return 0;
}
return 1;
} int main(void)
{
int n, i;
int a[M], b[M];
int step; while (scanf("%d", &n) != EOF && n)
{
for(i=0; i<n; i++)
scanf("%d", &a[i]);
step = 0;
while (!shouldEnd(a, n))
{
//printf("step = %d\n", step);
for (i=0; i<n; i++)
{
b[i] = (a[i]>>1)+(a[(n+i-1)%n]>>1);
//printf("b[%d]=%d\n", i, b[i]);
}
for (i=0; i<n; i++)
{
a[i] = ((b[i]+1)>>1)<<1;
//printf("a[%d]=%d\n", i, a[i]);
}
step ++;
} printf("%d %d\n", step, a[0]);
} return 0;
}
/**************************************************************
Problem: 1145
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/
九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)的更多相关文章
- 九度OJ 1147:Jugs(罐子) (模拟、游戏)
时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:243 解决:200 题目描述: In the movie "Die Hard 3", Bruce Willis and ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
随机推荐
- Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为1,有两种方式,求最小花费/贪心]
B. Our Tanya is Crying Out Loud time limit per test 1 second memory limit per test 256 megabytes inp ...
- Java开发笔记(一百零三)线程间的通信方式
前面介绍了多线程并发之时的资源抢占情况,以及利用同步.加锁.信号量等机制解决资源冲突问题,不过这些机制只适合同一资源的共享分配,并未涉及到某件事由的前因后果.日常生活中,经常存在两个前后关联的事务,像 ...
- ios iPhone 如何将应用程序名称本地化
iPhone的应用程序名称也可以本地化,可以按照以下步骤来实施: 1. 修改项目目录下的’ -info.plist’文件名 将’ -info.plist’ 修改为 Info.plist 2. 将Inf ...
- php报错配置问题
在开发的时候php.ini ,要显示所有的错误 error_reporting=E_ALL | E_STRICT 在发布的时候可以显示除了notice之外的错误,打开错误记录功能 error_repo ...
- 高校师生福利!从现在起,可以免费申请LocaSpace和Wish3D的SDK!
目前,以管理空间数据见长的GIS已经在全球变化与监测.军事.资源管理.城市规划.土地管理.环境研究.灾害预测.交通管理.文物保护以及政府部门等许多领域发挥着越来越重要的作用. 如何开发出功能丰富又简洁 ...
- Java内存泄漏及分析
对于内存泄漏,首先想到的是C语言,其实不然,java中也有各种的内存泄漏.对于java程序员,在虚拟即中,不需要为每一个新建对象去delete/free内存,不容易出现内存泄漏.但是,正 是由于这种机 ...
- Oracle 查询一个表的所有字段
select * from user_tab_columns where table_name = 'T_B_CLIENT_MSG'
- 策略模式(headfirst设计模式学习笔记)
鸭子的行为被封装 进入一组类中,能够轻易的扩展和改变.假设须要能够执行时改变行为! 策略模式定义了算法族.分别封装起来.让他们能够相互替换,此模式让算法的变化独立于使用算法的客户. 继承,相似之处用继 ...
- gitlab服务器邮箱配置
如想用 SMTP 代替 Sendmail 发送email,添加如下相应邮箱服务商的配置到/etc/gitlab/gitlab.rb, 然后运行gitlab-ctl reconfigure使修改生效. ...
- Oracle 连接、会话数的查看,修改
http://blog.csdn.net/xiaoyao6650/article/details/4027041 查看processes #当前的连接数 select count(*) from v$ ...