1.大整数加法
题意:计算两个数得和
#include <bits/stdc++.h>
using namespace std;
int main()
{
char a[105],b[105];
int ans[105] = {1};
memset(ans,0,sizeof(ans));
scanf("%s",a);
scanf("%s",b);
for (int i = 0; i < strlen(a) ; i++)
{
ans[strlen(a)-i] += a[i] - \'0\';
}
for (int i = 0; i < strlen(b) ; i++)
{
ans[strlen(b)-i] += b[i] - \'0\';
}
ans[0] = max(strlen(a),strlen(b));
for (int i=1; i <= ans[0] ; i++)
{
if ( ans[i] < 10)continue;
ans[i+1] += ans[i] / 10;
ans[i] %= 10;
if (i+1 > ans[0]) ans[0] = i+1;
}
for (int i=ans[0]; i >= 1 ; i--)
{
printf("%d",ans[i]);
}
printf("\\n");
return 0;
}
2.大整数乘法
题意:计算2 ^1000 各位数字累加和
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int VAL = 1000;
int32_t main()
{
char a[N],b[N];
int ans[N] ;
ans[0] = 1;
ans[1] = 1;
for (int32_t i = 0; i < 100; i++)
{
for (int32_t j=1; j <= ans[0] ; j++)
{
ans[j] *= 1024;
}
for (int32_t j=1; j <= ans[0]; j++)
{
if (ans[j] < VAL) continue;
ans[j+1] += ans[j] / VAL;
ans[j] %= VAL;
if (j == ans[0]) ++ans[0];
}
}
int sum = 0;
for (int i=1; i <= ans[0]; i++)
{
while (ans[i])
{
sum += ans[i]%10;
ans[i] /= 10;
}
}
cout<<sum<<endl;
return 0;
}
注意:VAL 的设置:是让每位ans[i] 保存三位数,一定程度可以减少数组的长度