这题主要考察的就是大数相加,借助string可以很方便的做这道题。
#include<string>
#include<iostream>
using namespace std;
string fun(string a, string b)
{
int len1 = a.length();
int len2 = b.length();
if (len1 > len2)
{
for (int i = 0; i < len1 - len2; i++)
{
b = "0" + b;
}
}
else
{
for (int i = 0; i < len2 - len1; i++)
{
a = "0" + a;
}
}
len1 = a.length();
int c=0;
int temp;
string ans;
for (int i = len1 - 1; i >= 0; i--)
{
temp = a[i] - '0' + b[i] - '0' + c;
c = temp / 10;
temp = temp % 10;
ans = char(temp + '0') + ans;
}
if (c != 0)
{
ans = char(c + '0') + ans;
}
return ans;
}
int main()
{
string str[1005];
str[1] = "1";
str[2] = "1";
for (int i = 3; i <= 1000; i++)
{
str[i] = fun(str[i - 1], str[i - 2]);
}
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int index;
cin >> index;
cout << str[index] << endl;
}
return 0;
}