时间限制: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. Codeforces 899 A.Splitting in Teams

      A. Splitting in Teams   time limit per test 1 second memory limit per test 256 megabytes input sta ...

  2. 有关javaScript面向对象和原型笔记

    javaScript是一种比較特殊的语言,ECMAScript中没有类的概念.跟其它面向对象的语言有一定的差别.它的对象也与基于类的语言中的对象有所不同,严格来说,javascript对象是一组没有特 ...

  3. hduoj1285确定比赛名次

     确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. django网站搭建常用的一些代码

    from functools import wrapsdef check_user_login(func): @wraps(func) def return_wrapper(request, *arg ...

  5. [转]Windows10内置Linux子系统初体验

    Windows10内置Linux子系统初体验 https://www.jianshu.com/p/bc38ed12da1d

  6. Linux下快速安装Mysql及使用

    1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查看有没有安装包: yum list mysql* 安装mysql客户端: ...

  7. 控制div属性

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 【SharePoint】SharePoint2013中使用客户端对象模型给用户控件赋初值

    本文要实现的功能:新建一条列表记录,打开新建记录画面时,自动给[申请人]赋值为当前登录用户. 在SharePoint2010中,可以使用SPServices的SPFindPeoplePicker方法来 ...

  9. quartz 应用到 spring定时任务 执行两次

    https://my.oschina.net/superkangning/blog/467487

  10. 我的IT之路

    在写这篇文章的时候内心是无比激动,因为这辈子是注定和IT打交道了. 都说大学时光是美好的,但却只有到了大四才知道时间是短暂的,也许和许多人一样,我的大学主要时光是在游戏中度过,1000多把的寒冰算是同 ...