【FOJ2206 11月月赛B】【观察找规律】函数求解 分析函数求值

 Problem 2206 函数求解

Accept: 28    Submit: 46
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

2120150001

 Sample Output

201520152014

 Source

FOJ有奖月赛-2015年11月


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
const int V=20150001;
int n,ans;
int f(int x)
{
	if(x<V)return x+2014;
	else return f(f(x-2015));
}
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;casei++)
	{
		scanf("%d",&n);//printf("%d\n",f(n));
		if(n<V)ans=n+2014;
		else ans=20152014;
		printf("%d\n",ans);
	}
	return 0;
}
/*
【题意】
给你一个函数f()有f[n]=
n+2014,n<20150001,
f( f(n-2015) ),n>=20150001。
每次给你一个n,(1<=n<=2015000000),让你输出f(n)。

【类型】
暴力找规律
观察分析结论

【分析】
这道题,一开始想起来感觉有些懵,然后怕耽误时间,
于是我干脆直接把对f()的定义写成了函数,然后就找到规律AC掉了,规律是这样的——

	if(n<V)ans=n+2014;
	else ans=20152014;

为什么呢?设V=20150001;
首先,n<V时ans=n+2014很好理解。
然后,n>=V时,我们的值会变为f(f(x-2015))的结果。
当x-2015>=V时,迭代会继续下去
当x-2015<V时,相当于先减2015再加2014,即变成--x;
这样我们会一直操作,直到x=20150001,然后答案会变成f(20150000)
于是最后的答案会锁定在20152014

*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值