【codevs 1011】数的计算

来源:互联网 发布:屏幕特效软件 编辑:程序博客网 时间:2024/05/21 18:48

1011 数的计算 2001年NOIP全国联赛普及组
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 白银 Silver
题解
题目描述 Description
我们要求找出具有下列性质数的个数(包含输入的自然数n):

先输入一个自然数n(n<=1000),然后对此自然数按照如下方法进行处理:

  1. 不作任何处理;

  2. 在它的左边加上一个自然数,但该自然数不能超过原数的一半;

  3. 加上数后,继续按此规则进行处理,直到不能再加自然数为止.

输入描述 Input Description
一个数n

输出描述 Output Description
满足条件的数的个数

样例输入 Sample Input
6

样例输出 Sample Output
6

数据范围及提示 Data Size & Hint
6个数分别是:

6

16

26

126

36

136

模拟

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n;int f(int x){    if(x == 0)        return 1;    else     {        if(x % 2 == 1)            return f(x - 1);        else            return f(x / 2) + f(x - 1);    }}int main(){    scanf("%d",&n);    printf("%d",f(n));    return 0;}
0 0