hdu1034 簡單類比

來源:互聯網
上載者:User

標籤:style   os   io   for   re   代碼   

這裡開一個二維數組,num[105][2];   我也不知道N有多少,隨便開的,  那麼這裡num[i][0] 表示當前 第 i 個人擁有的糖果數,num[i][1]表示他上面一個人分給他的糖果數,具體實現見代碼注釋

這裡要注意的就是:先給糖果,然後如果有人糖果數為奇數,就加1 ,然後再判斷是不是相等

上馬:

#include <iostream>#include <cstring>using namespace std;int num[105][2];int N;bool end()//判斷是否end過程中,處理分糖果{    bool flag = true;//是否end    for(int i = 0; i < N; i ++)    {        num[i][0] = num[i][0] + num[i][1]; //先處理        if(num[i][0] % 2 != 0) num[i][0] ++;//奇數就加1    }    for(int i = 0; i < N-1; i ++)    {        if(num[i][0] != num[i+1][0]) flag = false;//兩兩中相等就都相等    }    return flag;}int main(){    while(cin >> N && N)    {        memset(num,0,sizeof(num));        for(int i = 0; i < N; i ++) cin >> num[i][0];        int time = 0;//次數        while(1)        {            if(end()) break;            time ++;            for(int i = 0; i < N; i ++)//迴圈處理            {                int right = (i+1)%N; //這裡表示下一個人                num[right][1] = num[i][0] / 2;                num[i][0] = num[i][0]/2;            }        }        cout << time << ' ' << num[0][0]<<endl;    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.