代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[210][400];
char str[4000];
//大数求和
char *bigNumberSumAandB(char *a, char *b)
{
memset(str, 0, sizeof(str));
int lengthA = (int)strlen(a);
int lengthB = (int)strlen(b);
int bigerLength = lengthA > lengthB ? lengthA : lengthB;
int jinwei = 0;
int tempSum = 0;
int i = 0;
for(i = 0; i < bigerLength; i++) {
int numberA = a[i] - 48;
int numberB = b[i] - 48;
//由于某个字符串可能比较短,那么它的末尾会是'\'
if (a[i] < 48 || a[i] > 57) { //a的长度小于b的情况下
tempSum = numberB + jinwei;
} else if (b[i] < 48 || b[i] > 57) {//a的长度大于b的情况下
tempSum = numberA + jinwei;
} else {
tempSum = numberA + numberB + jinwei;
}
if (tempSum <= 9) {
str[i] = tempSum + 48;
jinwei = 0;
} else {
str[i] = tempSum % 10 + 48;;
jinwei = 1;
}
}
if (jinwei != 0) {
str[i] = jinwei + 48;
}
return str;
}
int main(int argc, const char * argv[]) {
int n;
memset(s, 0, sizeof(s));
s[1][0] = '1';
s[2][0] = '2';
for (int i = 3; i <= 200; i++) {
strcpy(s[i], bigNumberSumAandB(s[i - 1], s[i - 2]));
}
while (~scanf("%d", &n)) {
int result = (int)strlen(s[n]);
int temp = 0;
for(int i = result - 1; i >= 0; i--) {
if (s[n][i] != 0) {
temp = i;
break;
}
}
for (int i = temp; i >= 0; i--) {
printf("%c", s[n][i]);
}
printf("\n");
}
return 0;
}