题目描述
我们要求找出具有下列性质数的个数(包含输入的正整数 nn)。
先输入一个正整数 nn(n \le 1000n≤1000),然后对此正整数按照如下方法进行处理:
不作任何处理;
在它的左边加上一个正整数,但该正整数不能超过原数的一半;
加上数后,继续按此规则进行处理,直到不能再加正整数为止。
输入格式
1个正整数 (n≤1000)
输出格式
1 个整数,表示具有该性质数的个数。
输入输出样例
输入 #1复制
6输出 #1复制
6说明/提示
满足条件的数为
6,16,26,126,36,136
【题目来源】
NOIP 2001 普及组第一题
解题目的:获得符合条件的数字个数
题目类型:递推
解题思路: f[i] = 1;
f[i] += f[j]; (j<=i/2)
注意点:算上数字本身;
代码:
方法1:
#include <bits/stdc++.h>
#define MAXN 1e6+2
#define inf 0x3f3f3f3f
#define rep(x, a, b) for(int x=a; x<=b; x++)
#define per(x, a, b) for(int x=a; x>=b; x--)
using namespace std;
const int NC = 1e5+2;
int cot = 0;
int f[1001];
int main()
{
int n;
scanf("%d", &n);
rep(i, 1, 1000)
{
f[i] = 1;
rep(j, 1, i/2)
{
f[i] += f[j];
}
}
cout<<f[n];
return 0;
}
方法2:递归(本题会超时)
#include <bits/stdc++.h>
#define MAXN 1e6+2
#define inf 0x3f3f3f3f
#define rep(x, a, b) for(int x=a; x<=b; x++)
#define per(x, a, b) for(int x=a; x>=b; x--)
using namespace std;
//const int NC = 1e5+2;
int cot = 0;
void f(int cur_)
{
if(cur_ != 1)
{
rep(i, 1, cur_/2)
{
f(i);
cot++;
}
}
return ;
}
int main()
{
//ios::sync_with_stdio(false);
int n;
scanf("%d", &n);
f(n);
printf("%d", cot+1);
return 0;
}