part 1
高精度练习之加法
注意:位数 不同吧 还有可能比 位数最长的还要长
#include<cstdio>
#include<algorithm>
#include<cstring>
//by mars_ch
using namespace std;
char a[1005],b[1005];
int ans[1005],tot;
int main()
{
scanf("%s",a);
scanf("%s",b);
int l1=strlen(a);
int l2=strlen(b);
int maxx=max(l1,l2);
if(l2 == maxx)
{
swap(l1,l2);
for(int i=0;i<maxx;i++)
{
swap(a[i],b[i]);
}
}
for(int i=1;i<=maxx;i++)
{
int p1=l1-i;
int p2=l2-i;
//printf("%c %c\n",a[p1],b[p2]);
if(p2>=0)
{
ans[++tot]+=a[p1]-'0'+b[p2]-'0';
}
else
{
ans[++tot]+=a[p1]-'0';
}
//printf("now number is %d\n",ans[tot]);
if(ans[tot]>=10) ans[tot+1]++;
ans[tot]%=10;
}
if(ans[tot+1]) tot++;
for(int i=tot;i>=1;i--)
{
printf("%d",ans[i]);
}
puts("");
return 0;
}
part2
乘法
模拟数式运算
主要在于 乘完之后加在哪位写挂了。
a1-i+a2-j-1
这个东西吗
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int a[10000];
string s1,s2;
void addd()
{
int a1=s1.size(),a2=s2.size();
for (int i=0;i<a1;i++)
for (int j=0;j<a2;j++)
a[a1-i+a2-j-1]+=(s1[i]-'0')*(s2[j]-'0');
for (int i=1;i<=a1+a2;i++) { a[i+1]+=a[i]/10; a[i]%=10; }
a[0]=a1+a2; while (a[a[0]]==0&&a[0]>1) a[0]--;
}
int main()
{
cin>>s1>>s2;
memset(a,0,sizeof(a));
addd();
for (int i=a[0];i>=1;i--) cout<<a[i];
cout<<endl;
return 0;
}
暂时就这样吧 睡觉了
明天更新减除