您的位置:首页 > 其它

hoj 1533 Fibonacci Numbers

2012-03-30 14:53 295 查看
http://acm.hit.edu.cn/hoj/problem/view?id=1533

用字符串表示整数,进行大数加法运算

/*This Code is Submitted by billforum for Problem 1533 at 2012-03-30 14:50:02*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stack>
using namespace std;

int main()
{
string t1,t2,t;
int z;
int num;
while(scanf("%d",&num)!=EOF)
{
if(num==1||num==2)
{
printf("1\n");
}
else
{
t1="1";
t2="1";
for(int k=3;k<=num;k++)
{
int i,j;
z=0;
t="";
for(i=t1.length()-1,j=t2.length()-1;i>=0;i--,j--)
{
t+=(char)((t1[i]-48+t2[j]-48+z)%10+48);
z=(t1[i]-48+t2[j]-48+z)/10;
}
for(i=j;i>=0;i--)
{
t+=(char)((t2[i]-48+z)%10+48);
z=(t2[j]-48+z)/10;
}
if(z>0)
{
t+=(char)(z+48);
}
t1="";
t1=t2;
t2="";
for(int ii=t.length()-1;ii>=0;ii--)
t2+=t[ii];
}
cout<<t2<<endl;
}
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: