CUC-SUMMER-4-A

A - Candy Sharing Game
HDU - 1034

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.

題意:小朋友們坐成圈,當老師吹口哨時,每個小朋友把自己一半的糖給右邊的人伞广,然后老師給糖數(shù)為單數(shù)的小朋友一顆糖驾中,重復這個過程,直到所有小朋友糖數(shù)相等分衫,求一共吹了多少次口哨场刑,最終每個小朋友幾個糖。

解法:水題蚪战,一個循環(huán)就可以了牵现。

代碼:

#include<iostream>
using namespace std;

int main()
{
    int num;
    while(cin>>num&&num!=0){
        int a[100];
        int ans=0;
        for(int i=0;i<num;i++)
            cin>>a[i];
        while(1){
            int flag=1;
            for(int i=1;i<num;i++)
                if(a[i]!=a[i-1])
                    flag=0;
            if(flag==1)
                break;
            int temp=a[0];
            for(int i=0;i<num;i++){
                a[i]/=2;
                if(i==num-1)
                    a[i]+=temp/2;
                else
                    a[i]+=a[i+1]/2;
                if(a[i]%2==1)
                    a[i]++;
            }
            ans++;
        }
        cout<<ans<<" "<<a[0]<<endl;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市邀桑,隨后出現(xiàn)的幾起案子瞎疼,更是在濱河造成了極大的恐慌,老刑警劉巖壁畸,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贼急,死亡現(xiàn)場離奇詭異茅茂,居然都是意外死亡,警方通過查閱死者的電腦和手機太抓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門空闲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人走敌,你說我怎么就攤上這事碴倾。” “怎么了悔常?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵影斑,是天一觀的道長。 經(jīng)常有香客問我机打,道長矫户,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任残邀,我火速辦了婚禮皆辽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘芥挣。我一直安慰自己驱闷,他們只是感情好,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布空免。 她就那樣靜靜地躺著空另,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蹋砚。 梳的紋絲不亂的頭發(fā)上扼菠,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機與錄音坝咐,去河邊找鬼循榆。 笑死,一個胖子當著我的面吹牛墨坚,可吹牛的內(nèi)容都是我干的秧饮。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼泽篮,長吁一口氣:“原來是場噩夢啊……” “哼盗尸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起咪辱,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤振劳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后油狂,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體历恐,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡寸癌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了弱贼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒸苇。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖吮旅,靈堂內(nèi)的尸體忽然破棺而出溪烤,到底是詐尸還是另有隱情,我是刑警寧澤庇勃,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布檬嘀,位于F島的核電站,受9級特大地震影響责嚷,放射性物質(zhì)發(fā)生泄漏鸳兽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一罕拂、第九天 我趴在偏房一處隱蔽的房頂上張望揍异。 院中可真熱鬧,春花似錦爆班、人聲如沸衷掷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽戚嗅。三九已至,卻和暖如春枢舶,著一層夾襖步出監(jiān)牢的瞬間渡处,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工祟辟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人侣肄。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓旧困,卻偏偏與公主長得像,于是被迫代替她去往敵國和親稼锅。 傳聞我的和親對象是個殘疾皇子吼具,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容