大数加法

#include
#include
using namespace std;


const int MAXN = 1000;
int a[MAXN] = { 0 };
int b[MAXN] = { 0 };


int main()
{
string str1, str2;
cin >> str1 >> str2;
long int len1, len2;
int up = 0;
int number = 0;

len1 = str1.length();
len2 = str2.length();




for (int i = len1-1,k = 0;i >= 0;i--,k++)
{
a[k] = str1[i] - '0';
}
for (int i = len2 - 1,k = 0;i >= 0;i--, k++)
{
b[k] = str2[i] - '0';
}
for (int i = 0;i < MAXN;i++)
{
a[i] = a[i] + b[i] + up;
up = a[i] / 10;
a[i] = a[i] % 10;
}




for (number = MAXN - 1;number >= 0;number--)
{
if (a[number])
break;
}
for (int i = number;i >= 0;i--)
{
cout << a[i];
}


return 0;
}

你可能感兴趣的:(算法)