ZJANS

b911. 我想跟Kevin借筷子系列4

Beginner Last Update: 2026/01/21
cpp

# 題目簡述


解法一、迴圈

✅ 完整代碼

評分結果(參考) : AC (2ms, 336KB)

#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    while(cin >> n){
        int ans = 0;
        while(n > 0){
            n /= 2;
            ans++;
        }
        cout << ans << '\n';
    }
    
    return 0;
}

解法二、數學

✅ 完整代碼

評分結果(參考) : AC (2ms, 336KB)

#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    while(cin >> n){
        cout << floor(log2(n)) + 1 << '\n';
    }
    
    return 0;
}