UVa 11384 Help is needed for Dexter (递归)

题意:给定一个n表示1到n的序列,让你用最小的步数把这个序列都变为0,每个操作可以从序列中选择一个或多个个,同时减掉一个正整数,求最少的步数。

析:一看这个题,感觉挺高深的,但是静下心来想想,其实挺简单。和二分思想有点像,你可把n/2到n的数减掉一个数,变成和前n/2个是一样,然后重复操作,直到全为0。

代码如下:

#include <iostream>
#include <cstdio>

using namespace std;
int f(int n){ return 1 == n ? 1 : f(n/2) + 1;  }

int main(){
    int n;
    while(~scanf("%d", &n)){
        int ans = f(n);
        cout << ans << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5529103.html