HDU-1034-Candy Sharing Game(C++ && 簡單類比)

來源:互聯網
上載者:User

標籤:acm   c++   簡單類比   精闢   短小精悍   

Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3703    Accepted Submission(s): 2311


Problem DescriptionA 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.
 
InputThe 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.
 
OutputFor 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
6362222211222018161412108642424680
 
Sample Output
15 1417 224 8HintThe 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. 
 
SourceGreater New York 2003 
RecommendWe have carefully selected several similar problems for you:  1035 1037 1038 1018 1028 

參照了網上的方法,不得不說,實在是精闢!精簡併且容易理解,學習了!
首先解釋一下題目:
一定數量的學生圍成一個圈,老師站在圓的中心。每個學生最開始有偶數個糖果數。當老師吹一聲哨子的時候,每一個學生同時給自己右邊的鄰居一半的糖果數。所有的學生,在執行這個處理之後,如果他當前的糖果數是奇數個,那麼他就可以從老師手中格外獲得一個糖果。這個遊戲結束的條件是所有的學生都擁有相同的糖果數。寫一個程式來求出遊戲結束的時候老師吹口哨的次數和每個學生擁有的相同糖果數是多少。
祝室友程教主,和老趙北京藍橋之戰大獲全勝!!接下來上每日一水代碼(ps.其實好像沒有保證每天都有做......):
//這題是一道簡單類比,注意處理邊界條件就行了!#include<iostream>#include<cstdio>#define N 100using namespace std;int main(){    int s[N],n,i,k,t,p;    while(scanf("%d",&n) == 1 && n)    {        for(i=0;i<n;i++)            scanf("%d",s+i);        for(k=1;;k++)        {            t=s[n-1]/2;                       //首先t存取最後一個人糖果數的一半,即處理邊界條件!            for(i=0;i<n;i++)            {                p=s[i]/2;                     //p存取當前糖果數的一半用來給右邊的同學                s[i]=s[i]/2+t;                //自己減一半再加上左邊同學給的一半                t=p;                          //給完一個同學當然換下一個                if(s[i]%2!=0)                 //當前處理完如果為奇數,那就還得收老師一個                    s[i]++;            }            t=s[0];            for(i=1;i<n&&s[0]==s[i];i++);     //如果每個同學擁有的糖果數相同則遊戲結束!            if(i==n)            {                break;            }        }        printf("%d %d\n",k,t);                //輸出遊戲次數和每個人最終的糖果數    }    return 0;}


 

HDU-1034-Candy Sharing Game(C++ && 簡單類比)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.