【CODE[VS]】1011--数的计算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34072526/article/details/87895897

题目描述 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>
using namespace std;

int main(){
    int n, i;
    int a[1010] = {0};
    cin >> n;
    a[0] = a[1] = 1;
    for(i = 2; i <= n; i++){
        if(i % 2 != 0) a[i] = a[i - 1];
        else a[i] = a[i - 2] + a[i / 2];
    }
    cout << a[n];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34072526/article/details/87895897
今日推荐