jiangyuan2011 @ 2024-07-28 10:09:02
#include <bits/stdc++.h>
using namespace std;
long long int m,n;
int num1[505],num2[505],jinwei[505],res[505],s,u,w=0,v=0;
string s1,s2;
void plus1(){
while (m)
{
//得到当前整数 尾位数
s = m % 10;
//切割这一整形
m /= 10;
num1[w]=s;
w++;
}
while (n)
{
u = n % 10;
n /= 10;
num2[v]=u;
v++;
}
int l1=w;
int l2=v;
int l3=max(l1,l2)+1;
for(int i=0;i<l3;i++){
res[i]=(num1[i]+num2[i]+jinwei[i])%10;
jinwei[i+1]=(num1[i]+num2[i]+jinwei[i])/10;
}
if(res[l3-1]==0) l3--;
for(int i=l3-1;i>=0;i--){
cout<<res[i];
}
}
int plus2(int a,int b){
int ab=a+b;
if(a==0)return b;
else if(b==0)return a;
else return ab;
}
int main(){
cin>>m>>n;
if(m<=4294967295&&n<=4294967295){
cout<<plus2(m,n);
}else{
plus1();
}
return 0;
}
by Enoch2013 @ 2024-07-28 10:15:23
@jiangyuan2011 你自己看一下数据范围
by PYM20111102 @ 2024-07-28 11:48:02
int以内, a+b即可
by ly147146143 @ 2024-07-28 16:46:31
@jiangyuan2011 有负数
by NAREKIB @ 2024-08-14 15:57:09
@PYM20111102 高精度
by PYM20111102 @ 2024-08-14 17:31:57
@bikeran 不需要
by PYM20111102 @ 2024-08-14 17:36:01
#include<bits/stdc++.h>
using namespace std;
int a, b;
int main () {
cin >> a >> b;
cout << a + b << endl;
return 0;
}
by PYM20111102 @ 2024-08-14 17:37:18
@bikeran 满分
by NAREKIB @ 2024-08-14 20:49:52
@PYM20111102 ```
using namespace std; int a[2002],b[2002],c[2002],n=0; bool out=0; void nixu(bool x,string z)
{
int g=0;
if(x==0)
{
for(int i=z.length()-1;i>=0;i--)
{
a[g]=z[i]-48;
g++;
}
}
else
{
for(int i=z.length()-1;i>=0;i--)
{
b[g]=z[i]-48;
g++;
}
}
}
string jia(string a1,string b1)//2.逆序存储,3加法,4.从大数末尾开始,直到遇到第一个数字时才开始输出
{
//2
n=max(a1.length(),b1.length());
for(int i=0;i<=n+2;i++)
{
a[i]=0;
b[i]=0;
}
string end="";
nixu(0,a1);
nixu(1,b1);
int g=0;
for(int i=0;i<n;i++)//加法主体
{
if(a[i]+b[i]+c[i]>=10)
{
c[i]=a[i]+b[i]+c[i]-10;
c[i+1]+=1;
}
else
{
c[i]=a[i]+b[i]+c[i];
}
}
if(a[n-1]+b[n-1]>=10)n++;
for(int i=n-1;i>=0;i--)
{
if(c[i]!=0)out=1;
if(out==1)end+=char(c[i]+48);
g++;
}
return end;
}
int main()
{
string a,b;
cin>>a>>b;
cout<<jia(a,b);
}
固若金汤
by PYM20111102 @ 2024-08-16 14:05:35
@NAREKIB 有点麻烦