Problem 2206 函数求解
Accept: 161 Submit: 494
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
给出n,求f(n)。
Input
第一行一个正整数T,表示数据组数。 接下来T行,每行一个正整数n。 T<=20,n<=2015000000。
Output
对于每组数据,输出一行f(n)。
Sample Input
2
1
20150001
Sample Output
2015
20152014
Source
FOJ有奖月赛-2015年11月
Submit Back Status Discuss
首先,这个题一开始是用递归做的,不过交上去一直运行错误,所以就重新开始测试样例,结果发现大于20150001的数应该输出的结果都为20152014.自己又模拟推了一下,正确。
推导过程如下
f(20150002)=f(f(20150002-2015))=f(20150001)=20152014;
其实不难发现,大于20150001的n,,f(n)=f(20150001);
先附上RUNTIMEERROR的代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long sum=0;
void ditui(long long n,long long k)
{
if(n>=20150001)
{
n=n-2015;
ditui(n,k+1);
}
else
{
n=n+2014;
if(k==0)
{
sum=n;
return ;
}
ditui(n,k-1);
}
}
int main()
{
long long t,n;
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
if(n<20150001)
{
printf("%lld\n",n+2014);
}
else
{
ditui(n,0);
printf("%lld\n",sum);
}
}
return 0;
}
下面附上AC代码,。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n<20150001)
{
printf("%d\n",n+2014);
}
else
{
printf("20152014\n");
}
}
return 0;
}