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 <iostream>
using namespace std;
int x[],y[];
void f(int s[],int n,int k)
{
int i,j,a[][];
for(i=;i<n;i++)a[][i]=s[i];
for(j=;;j++){
bool flag=true;
a[j][]=a[j-][]/+a[j-][n-]/;
if(a[j][]%!=)a[j][]++;
for(i=;i<n;i++){
a[j][i]=a[j-][i-]/+a[j-][i]/;
if(a[j][i]%!=)a[j][i]++;
if(flag==true&&a[j][i]!=a[j][])flag=false;
}
if(flag==true){
x[k]=j;
y[k]=a[j][];
break;
}
}
}
int main()
{
int n,k=;
while(cin>>n){
if(n==)break;
int s[],i;
for(i=;i<n;i++)cin>>s[i];
f(s,n,k);
k++;
}
for(int i=;i<k;i++)
cout<<x[i]<<" "<<y[i]<<endl;
//system("pause");
return ;
}

这是正确代码
#include<iostream>
using namespace std;
const int MAXN=;
int a[MAXN];
int main()
{
int n;
int i;
while(cin>>n,n)
{
for(i=;i<n;i++)cin>>a[i];
int res=;
while()
{
for(i=;i<n;i++)
if(a[i-]!=a[i]) break;
if(i>=n) break;
res++;
int temp=a[n-]/;
for(i=n-;i>;i--)
{
a[i]/=;
a[i]+=a[i-]/;
}
a[]/=;
a[]+=temp;
for(i=;i<n;i++)
if(a[i]&) a[i]++;
}
cout<<res<<" "<<a[]<<endl;
}
//system ("pause");
return ;
}

M - Candy Sharing Game的更多相关文章

  1. POJ - 1666 Candy Sharing Game

    这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...

  2. hdu 1034 Candy Sharing Game

    Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. Candy Sharing Game(模拟搜索)

    Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. Candy Sharing Game(hdoj1034)

    Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...

  5. HDU1034 Candy Sharing Game

    Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...

  6. HDU 1034 Candy Sharing Game (模拟)

    题目链接 Problem Description A number of students sit in a circle facing their teacher in the center. Ea ...

  7. sicily 1052. Candy Sharing Game

    Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description A number of students sit in a circle ...

  8. 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:248 解决:194 题目描述: A number of students sit in a circle facing their teac ...

  9. HDU-1034 Candy Sharing Game 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1034 水题 代码 #include <cstdio> #include <algorithm> ...

随机推荐

  1. 在js中使用json

    在js中使用json var obj = {     "1" : "value1",     "2" : "value2" ...

  2. windows安装composer方法和使用方法

    最近在学习yii2的框架的相关知识,对于yii2的许多新特性,最好还是去查看官网文档最好,如果有中文翻译的网站就更好了. 学习yii2的第一个门槛就是得安装composer这个依赖管理工具(但目前我认 ...

  3. include 和 require 的区别

    1. 首先不去介绍大家都知道的区别,百度上都进行了详细的说明,对于返回值的方面大家都很少提到. include 和 require 还有一个区别就是是否具有返回值.参见手册 对include 加载文件 ...

  4. 百度ueditor 上传图片后如何设置样式

    最近项目中遇到一个问题,UEditor上传图片后,在内容展示会修改图片样式.但是表情也是img标签,所以全局修改是有问题的, 所以只能着手修改一下插件的代码. 首先找到图片上传的服务器段文件.这里主要 ...

  5. 最短路--hdu2544

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. Android广播机制(转)

    1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...

  7. 安装ArchLinux的参考分区方案

    其实就是从Archwiki上搬运过来的 = =. 分区方案 虽然有一些关于分区方案的通用建议,但没有严格的准则.有许多影响分区方案的因素,例如对灵活性的期望,访问速度,安全性以及可用磁盘空间的硬性限制 ...

  8. shell脚本中局部变量local

    shell脚本中局部变量 在shell中定义函数可以使代码模块化,便于复用代码.不过脚本本身的变量和函数的变量的作用域问题可能令你费解,在这里梳理一下这个问题. (1)Shell脚本中定义的变量是gl ...

  9. 【转】Linux中history历史命令使用方法详解

    原文网址:http://os.51cto.com/art/201205/335040.htm 当你在玩Linux的时候,如果你经常使用命令行来控制你的Linux系统,那么有效地使用命令历史机制将会使效 ...

  10. checkbox全选和反选

     $("#CheckBox").click(function () {                 if (this.checked) {                    ...