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. cookie自封装对象

    cookie.js(设置名值对属性时候不支持设置成前后有空格的格式,如' key'或'key ',只支持‘key’) (function initCookieClass(win){// 定义匿名函数并 ...

  2. OpenGL ES 2.0 光照

    基本的光照 光照分成了3种组成元素(3个通道):环境光.散射光以及镜面光. 材质的反射系数实际指的就是物体被照射处的颜色,散射光强度指的是散射光中的RGB(红.绿.蓝)3个色彩通道的强度. 环境光 指 ...

  3. BZOJ 2330 SCOI 2011 糖果

    2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MB Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友 ...

  4. Window7下手动编译最新版的PCL库

    PCL简介 PCL是Point Cloud Library的缩写,是一个用于处理二维图像,三维深度图像和三维点云的C++库.该库是完全开源的,可免费用于商业和学术研究. 官方网站:http://poi ...

  5. 慕课Linux学习笔记(三)系统分区

    Linux的系统分区 主分区:最多四个 扩展分区:最多只能一个,不能写入数据,只能包含逻辑分区 逻辑分区 必须分区: / (根分区) Swap 分区 (交换分区,内存的两倍,不超过2GB,如果内存大于 ...

  6. js实现IE/Firefox的收藏功能

    代码: function addFavorite(el){ var url = window.location.href, title = $('head').find('title').html() ...

  7. 插件的理解$.extend()与$.fn.extend()

    插件的理解.$.extend()与$.fn.extend()    插件开发包括两种:1.类级别的插件开发,即$.extend()扩展jquery对象本身:2.对象级别的插件开发,即$.fn.exte ...

  8. 数据画图 jpgraph & chart.js

    今天想到要研究下“用图表的形式来呈现数据”这个主题.对比了下两种实现的方法: 方法一:通过php代码在服务器端生成图像,再将图像传回客户端.使用jpGraph类库. 方法二:通过js和html5技术, ...

  9. ashx中session的使用

    在平常的页面上是用是很容易就的到request,response对像,从而对其进行一些操作,但在ashx(一般处理程序)中却是有一点的不同, 在ashx你无法正常的使用session,即 contex ...

  10. php 远程下载图片到本地

    大家好,从今天开始,小弟开始写写博客,把自己在工作中碰到的问题的解决方法纪录下来,方便以后查找,也给予别人方便,小弟不才,第一次写博客,有什么不足之处请指出,谢谢! 今天纪录的是怎么通过PHP远程把图 ...