#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
char Fib[5001][1100] = {"0","1","1"};
void add(char a[],char b[], char c[])
{
int len_a = strlen(a),len_b = strlen(b);
int len_c = len_a > len_b ? len_a : len_b;
++len_c;
memset(c,'0',len_c);
memset(a+len_a,'0',len_c-len_a);
memset(b+len_b,'0',len_c-len_b);
int carry = 0,temp = 0,i;
for (i = 0; i < len_c; ++i)
{
temp = a[i] - '0' + b[i] -'0' + carry;
carry = temp / 10;
c[i] += temp % 10;
}
if (carry > 0)
c[i] += carry,len_c++;
for (i = len_c-1; i >= 0 && c[i] == '0'; --i);
c[i+1] = 0;
len_c = i+1;
}
int main()
{
for (int i = 3; i <= 5000; ++i)
{
char buf1[1100] = {0},buf2[1100] = {0},buf3[1100] = {0};
strcpy(buf1,Fib[i-2]);
strcpy(buf2,Fib[i-1]);
reverse(buf1,buf1+strlen(buf1));
reverse(buf2,buf2+strlen(buf2));
add(buf1,buf2,buf3);
reverse(buf3,buf3+strlen(buf3));
strcpy(Fib[i],buf3);
}
int n;
freopen("d:\\UVa\\uva_in.txt", "w", stdout);
while (scanf("%d", &n) != EOF)
printf("%s\n", Fib[n]);
return 0;
}
UVa495 - Fibonacci Freeze
最新推荐文章于 2022-06-16 04:36:34 发布