题意:给定正整数n,求把1,2,……,n中所有书都变成0的最少操作次数,每次操作可从序列中选择一个或多个整数,同时减去一个相同的正整数。
分析:如1,2,3,4,5,6,
第一步将4,5,6同时减4,则变成了1,2,3,0,1,2
情况与1,2,3相同,由此可知f(n) = f(n / 2) + 1
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<string> 5 #include<cstdlib> 6 #include<cmath> 7 #include<sstream> 8 #include<algorithm> 9 #include<set> 10 #include<stack> 11 #include<list> 12 #include<map> 13 #include<queue> 14 #include<deque> 15 #include<vector> 16 #include<cctype> 17 using namespace std; 18 const int MAXN = 1000 + 10; 19 const int MAXT = 10000 + 10; 20 const int INF = 0x7f7f7f7f; 21 const double EPS = 1e-6; 22 const double PI = acos(-1.0); 23 typedef long long ll; 24 typedef unsigned long long llu; 25 int f(int n) 26 { 27 return n == 1 ? 1 : f(n / 2) + 1; 28 } 29 int main() 30 { 31 int n; 32 while(scanf("%d", &n) != EOF) 33 printf("%d\n", f(n)); 34 return 0; 35 }