首页 > 代码库 > UVa11384 Help is needed for Dexter (思维)

UVa11384 Help is needed for Dexter (思维)

链接:http://acm.hust.edu.cn/vjudge/problem/26096
分析:带几个栗子进去发现,序列1,2,...,n,为了平衡将(n/2+1)~n的数同时减去(n/2+1),得到1,2,...n/2,0,1,...(n-1)/2,它等价于1,2,...,n/2,因此f(n)=f(n/2)+1,边界是f(1)=1。

 1 #include <cstdio> 2  3 int f(int n) { 4     return n == 1 ? 1 : f(n / 2) + 1; 5 } 6  7 int main() { 8     int n; 9     while (scanf("%d", &n) == 1) {10         printf("%d\n", f(n));11     }12     return 0;13 }

 

UVa11384 Help is needed for Dexter (思维)