10334
其实就是到1000的斐波那契序列,开始开的串的长度200,竟然都能超。。。改成1000就对了。
稍微的难点就是用到大数加法。
//============================================================================ // Name : 10334.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n, t; long long a, b, c; char sa[1000], sb[1000], sc[1000]; //void add(char *a, char *b) //{ // int lena = strlen(a); // int lenb = strlen(b); // int len = lena > lenb ? lena : lenb; // len++; // memset(a+lena, '0', len-lena); // memset(b+lenb, '0', len-lenb); // int c, t; // c = 0; // for(int i = 0;i < len;i++) // { // t = a[i] - '0' + b[i] - '0' + c; // a[i] = t%10+'0'; // c = t/10; // } // if(a[len-1] == '0') // { // a[len-1] = 0; // } // strcpy(sc, a); //} void add(char *a, char *b){ int len1, len2, sans[1000]; char s1[1000], s2[1000], st[1000]; len1 = strlen(a); len2 = strlen(b); if(len1 > len2){ strcpy(st, a); strcpy(a, b); strcpy(b, st); } len1 = strlen(a); len2 = strlen(b); for(int i = 0;i < len1;i++){ s1[i] = a[len1-1-i]; } s1[len1] = '\0'; for(int i = 0;i < len2;i++){ s2[i] = b[len2-1-i]; } s2[len2] = '\0'; c = 0; for(int i = 0;i < len2;i++){ if(i < len1){ sans[i] = (s1[i]-'0'+s2[i]-'0'+ c)%10; c = (s1[i]-'0'+s2[i]-'0'+ c)/10; } else{ sans[i] = (s2[i]-'0'+c)%10; c = (s2[i]-'0'+c)/10; } } if(c != 0){ len2 += 1; sans[len2-1] = c; } for(int i = 0;i < len2;i++){ sc[i] = sans[len2-1-i]+'0'; } sc[len2] = '\0'; } int main() { freopen("a.txt", "r", stdin); while(scanf("%d", &n)!=EOF){ if(n == 0){ printf("1\n"); continue; } if(n == 1){ printf("2\n"); continue; } strcpy(sa, "1"); strcpy(sb, "2"); for(int i = 2;i <= n;i++){ add(sa, sb); strcpy(sa, sb); strcpy(sb, sc); } printf("%s\n", sc); } return 0; }