du熊学斐波那契I

题意很明确,先求出一段循环节,再对这段数求出n,

 

一开始我用的vector来求,可是忘记了内存问题,参考了别人的代码,才发现了问题,真是惭愧,向大神们致敬,

 

重新写的一个

 


#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int vis[100], n,num,p;

void cnt(int x, int y)
{

vis[0] = x;
vis[1] = y;
for(int i = 2; i <= 100; i++)
{
int nexta = vis[i-1]+vis[i-2];
if( nexta > 9 )
{
vis[i] = nexta/10;
vis[i+1] = nexta%10;
i++;
}
else
vis[i] = nexta;
if( vis[i-1] == vis[0] && vis[i] == vis[1] )
{
p = i-1;
break;
}
}
num = vis[ (n-1)%p ];
}

int main()
{
ifstream cin("in.txt");
int a, b, T;
cin>>T;
for(int i = 1; i <= T; i++ )
{
cin>>a>>b>>n;
cnt(a,b);
cout<<"Case #"<<i<<": "<<num<<endl;

}
return 0;
}

 

posted on 2012-12-13 14:26  元点之始  阅读(184)  评论(0编辑  收藏  举报

导航