https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2379
http://7xjob4.com1.z0.glb.clouddn.com/33af24c925ae62df4c094b22a2ba7e37
题意:给定1-n的数,可选多个数减去同一整数,求最小的操作次数
思路:保留1~n/2,剩下全减去n/2+1,得1,2...,n/2,0,1,...,(n-1)/2.等价于1~n/2,所以f(n)=f(n/2)+1。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int f(int n) 5 { 6 if(n==1) return 1; 7 return f(n/2)+1; 8 } 9 10 int main() 11 { 12 int n; 13 int i,j,k; 14 while(scanf("%d",&n)!=EOF) 15 { 16 printf("%d ",f(n)); 17 } 18 return 0; 19 }