(递推) codeVs1011 && 洛谷P1028 数的计算

题目描述 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

找规律,先手写1到10的情况,找找规律

C++代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 1002;
int a[maxn];
int fun(int n){
    a[0] = 1;
    a[1] = 1;
    a[2] = 2;
    a[3] = 2;
    a[4] = 4;
    a[5] = 4;
    for(int i = 3; i <= n/2; i++){
        a[i*2] = a[i*2+1] = a[(i-1)*2] + a[i];
    }
    return a[n];
}
int main(){
    int n;
    cin>>n;
    cout<<fun(n)<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/Weixu-Liu/p/10630951.html