经过手动模拟可以发现,由于每个数字都不一样,貌似第一次只能消减一个数字,但是我们可以使得一些数字相等,然后后面就可以减去更多的数字。
那么我们取一半的地方,保留1~n/2,后面的数减去n/2+1,恰好后面的数也变成了1~n/2,那么也就等价于一个1~n/2的序列, 操作次数为一次。所以就不难写出递归程序:
f(n) = f(n/2) + 1; 边界是f(1) = 1。
细节参见代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 10000000;
const int maxn = 20 + 5;
int T,n,m;
int f(int n) {
return n == 1 ? 1 : f(n/2) + 1;
}
int main() {
while(~scanf("%d",&n)) {
printf("%d\n",f(n));
}
return 0;
}