Help is needed for Dexter

来源:互联网 发布:南京市软件行业协会 编辑:程序博客网 时间:2024/04/30 17:36

题目描述:

给定正整数 n,你的任务是用最少的操作次数吧序列 1,2,…,n 中的所有数都变成 0。每次操作可从序列中选择一个或多个整数,同时减去一个相同的正整数。

Input

Input consists of several lines each with N such that 1 ≤ N ≤ 1, 000, 000, 000. Input will be terminated
by end of file.

Output

For each N output L in separate lines.

Sample Input

1
2
3

Sample Output

1
2
2
经过打表(手动)发现,每次把序列的后半部分都减去一个最小值是最优的,比如序列 1,2,3,4,5,6 最优操作后就是1,2,3,0,1,2 ,递归处理就可以得到答案了,递归边界显然n == 1 return 1。

#include<iostream>#include<cstdio>using namespace std;int f(int n){    if(n == 1)        return 1;    return f(n/2) + 1;}int main(){    int n;    while(scanf("%d",&n) != EOF)        printf("%d\n",f(n));    return 0;}
0 0
原创粉丝点击