UVa 11636 - Hello World! 二分,水题 难度: 0

时间:2021-01-31 17:37:24

题目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2683

题意

原来有1个,每次可以任选数量成倍增长,问要操作多少次到n

思路

明显,先扩增到最大数位maxDigit,比如10,先扩增到8,然后再选择n - maxDigit扩增即可。因而结果为log2(n)

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<int, int> MyPair; int cntDigit(int n) {
int ans = ;
int x = ;
while (n > x) {
ans++;
x <<= ;
}
return ans;
} int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
int n;
for (int ti = ; scanf("%d", &n) == && n > ; ti++) {
printf("Case %d: %d\n", ti, cntDigit(n));
} return ;
}