题目描述 Description
最近cyz一直在研究一个函数,函数是这样的
If X<=100 F[x]=F[F[x+11]]
If X>=101 F[x]=X-10
现在cyz需要知道一些值对应的函数值。输入描述 Input Description
输入包括若干行(最多2500000行),每行一个正整数X(X≤106)。
最后一行以0结束.注意0不是要求的X,只是一个结束标记。输出描述 Output Description
对应每个要求的X值,每行输出一个对应的F[x]函数值。样例输入 Sample Input
100
101
0样例输出 Sample Output
91
91题解
刚开始看到题时想到的是记忆化搜索,于是这样就过了。
#include <cstdio>
using namespace std;
int f[101];
int F(int x)
{
if(x > 100) return x - 10;
if(f[x]) return f[x];
return f[x] = F(F(x + 11));
}
int main()
{
int x;
while(scanf("%d", &x) == 1)
if(x != 0) printf("%d\n", F(x));
return 0;
}
- 这其实是著名的91函数,x<=100时函数值一直是91……