D e s c r i p t i o n Description Description
给出一个函数:
f ( x ) = { f ( f ( x + 11 ) ) ( x ≤ 100 ) x − 10 ( x ≥ 101 ) f(x)=\left\{ \begin{aligned} f(f(x+11))\quad\quad(x\leq 100)\\ x-10\quad\quad\quad(x\geq101) \end{aligned} \right. f(x)={
f(f(x+11))(x≤100)x−10(x≥101)
I n p u t Input Input
输入文件包括若干行(最多 2500000 2500000 2500000行),每行一个正整数 X X X。
最后一行以 0 0 0结束。
注意 0 0 0不是要求的 X X X,只是一个结束标记。
O u t p u t Output Output
对应每个要求的 值,每行输出一个对应的 函数值。
S a m p l e Sample Sample I n p u t Input Input
100
101
0
S a m p l e Sample Sample O u t p u t Output Output
91
91
H i n t Hint Hint
对于 10 % 10\% 10%以内的数据, 0 ≤ X ≤ 10 0\leq X \leq10 0≤X≤10;
对于 30 % 30\% 30%以内的数据, 0 ≤ X ≤ 100 0\leq X \leq100 0≤X≤100;
对于 100 % 100\% 100%以内的数据, 0 ≤ X ≤ 1000000 0\leq X \leq1000000 0≤X≤1000000。
T r a i n Train Train o f of of T h o u g h t Thought Thought
只要 n n n小于 101 101 101就直接输出 91 91 91
反则输出 n − 10 n - 10 n−10
但是我这里还是用了记忆化搜
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;
ll F[1000250];
ll n;
ll Dfs(ll x)
{
if(x >= 101)return x - 10;
else
{
if(!F[x + 11])F[x + 11] = Dfs(x + 11);
if(!F[F[x + 11]])F[F[x + 11]] = Dfs(F[x + 11]);
return F[F[x + 11]];
}
}
int main()
{
scanf("%lld", &n);
while(n)
{
printf("%lld\n", Dfs(n));
n = 0;
scanf("%lld", &n);
}
}