先写出加法在写乘法
代码有点长,请耐心看完
#include <bits/stdc++.h>
using namespace std;
string he(string e, string f)
{
string r = "";
int a[1000] = {0};
int b[1000] = {0};
int c[1000] = {0};
int len = 0;
for (int i = 0; i < e.size(); i++)
{
a[i] = e[e.size() - i - 1] - '0';
}
for (int i = 0; i < f.size(); i++)
{
b[i] = f[f.size() - i - 1] - '0';
}
len = e.size() > f.size() ? e.size() : f.size();
for (int i = 0; i < len; i++)
{
c[i] += a[i] + b[i];
if (c[i] >= 10)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
if (c[len] != 0)
{
len++;
}
for (int i = 0; i < len; i++)
{
r += char(c[len - i - 1] + '0');
}
return r;
}
string cheng(string s)
{
string r = "";
int a[1000];
memset(a, 0, sizeof(a));
int len = s.size();
int rLen = len;
// S逆序存入int数组
for (int i = 0; i < len; i++)
{
a[i] = s[len - 1 - i] - '0';
}
for (int i = 0; i < len; i++)
{
a[i] *= 2;
}
for (int i = 0; i < len; i++)
{
if (a[i] >= 10)
{
a[i + 1] += a[i] / 10;
a[i] %= 10;
}
}
if (a[len] != 0)
{
rLen = len + 1;
}
for (int i = rLen - 1; i >= 0; i--)
{
r += char(a[i] + '0');
}
return r;
}
int main(int argc, char const *argv[])
{
string a = "1", b = "2", c;
int n;
cin >> n;
if (n == 1)
{
c = a;
}
else if (n == 2)
{
c = b;
}
else
{
for (int i = 3; i <= n; i++)
{
string t = cheng(b);
c = he(t, a);
a = b;
b = c;
}
}
cout << c;
return 0;
}