时间限制: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

来源:
2009年北京大学计算机研究生机试真题

思路:

该游戏一定能结束。主要是模拟好整个游戏过程,判断好结束条件。

代码:

#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(分享蜡烛游戏) (模拟)的更多相关文章

  1. 九度OJ 1147:Jugs(罐子) (模拟、游戏)

    时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:243 解决:200 题目描述: In the movie "Die Hard 3", Bruce Willis and ...

  2. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  3. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  4. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  5. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  6. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  7. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  8. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  9. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

随机推荐

  1. my.ini配置详解

    Mysql my.ini 配置文件详解 #BEGIN CONFIG INFO #DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 #TYPE: SYSTEM ...

  2. HTML DOM介绍

    HTML DOM定义了一系列的对象,以及访问和处理HTML的方法.通过DOM可以浏览所有的HTML元素,不但可以修改或者删除元素的文本和属性,而且可以创建新的元素. 一.首先对一个元素进行操作前,要得 ...

  3. ArcGIS 开发时,解决__类型“XX”同时存在于“”和“”中__的错误

    错误提示:类型“ESRI.ArcGIS.ADF.BaseClasses.BaseCommand”同时存在于“e:\Program Files\ArcGIS\DeveloperKit10.2\DotNe ...

  4. C++课程资源下载问题

    [来信] 贺老师,您好,我是江西某高校软件学院的一名在校学生.看了您在csdn上公布的博文和视频,我获益良多.不得不承认,之前的大学时光我是荒废了,立即就要大三了,我主攻的是C++方面.因此我悔过自新 ...

  5. xss跨站脚本攻击与防御读书笔记(原创)

    XSS在客户端执行 可以任意执行js代码 0x01  xss 的利用方式 1. 钓鱼      案例:http://www.wooyun.org/bugs/wooyun-2014-076685  我是 ...

  6. 知其一不知其二之Jenkins Hacking

    转自安全脉搏 本文首发安全脉搏 感谢大王叫我来巡山 的投递 转载请注明来源 大多安全工作者听到jenkins都会知道有个未授权的命令执行 但是如果Script页面要授权才能访问呢 或者你的用户没有Ov ...

  7. 【经验】使用Profiler工具分析内存占用情况

    Unity3D为我们提供了一个强大的性能分析工具Profiler.今天我们就使用Profiler来具体分析一下官方样例AngryBots的内存使用信息数据. 首先打开Profiler选择Memory选 ...

  8. 【Excle数据透视表】如何显示/隐藏数据透视表字段列表

    在创建完毕的数据透视表中,若单击数据透视表中任意单元格,即可显示数据透视表字段列表窗格,用户就可以通过弹出的窗格进行字段调整 两种情况需要隐藏数据数据透视表窗格 ①数据透视表已经完成 ②失误关闭窗格 ...

  9. Android中使用HttpURLConnection实现GET POST JSON数据与下载图片

    Android中使用HttpURLConnection实现GET POST JSON数据与下载图片 Android6.0中把Apache HTTP Client全部的包与类都标记为deprecated ...

  10. easyui datagrid自己定义操作列

    通过formatter方法给Jquery easyui 的datagrid 每行添加操作链接 我们都知道Jquery的EasyUI的datagrid能够加入而且自己定义Toolbar. 这样我们选择一 ...