2016年11月10日 星期四

TIOJ 1031 - Collecting Beans

http://tioj.infor.org/problems/1031

分堆的時候如果出現奇數就會被丟掉(?
所以能夠進桶的豆子就是比那個數字小的最大的2的冪次



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
    ios_base::sync_with_stdio(0);
    while(getline(cin,s)){
        if(s[0]=='0')break;
        stringstream ss;ss<<s;
        int ans=0,n;
        while(ss>>n && n){
            int i;
            for(i=1;i<=n;i<<=1){}
            ans+=(i>>1);
        }
        cout<<ans<<'\n';
    }
}

沒有留言:

張貼留言