Cutting Game FZU - 2268 二進制

題意:

給定一塊長度是n的金塊,求最少分成多少塊,使得1-n的所有數字都可以表示出來。

這一個是對二進制的理解。

#include <cstdio>
using namespace std;
int main() {
    int nTest; scanf("%d", &nTest);
    for(int t = 1; t <= nTest; t++) {
        int len; scanf("%d", &len);
        int cnt = 0;
        int now = 1;
        while(len > now) {
            len = len - now;
            now *= 2;
            cnt++;
        }
        printf("Case %d: %d\n", t, cnt + 1);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章