高精度(加法)
2017-11-03 20:22
155 查看
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=510;
char a[maxn],b[maxn];
int x[maxn],y[maxn],z[maxn];
int main(){
memset(z,0,sizeof(z));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
scanf("%s%s",a,b);
int l1=strlen(a),l2=strlen(b);
for(int i=1;i<=l1;i++) x[i]=a[l1-i]-'0';
//for(int i=1;i<=l1;i++) cout<<x[i]<<' ';
//cout<<endl;
for(int i=1;i<=l2;i++) y[i]=b[l2-i]-'0';
//for(int i=1;i<=l2;i++) cout<<y[i]<<' ';
//cout<<endl;
int l3=max(l1,l2),cur=0;
for(int i=1;i<=l3;i++){
z[i]+=x[i]+y[i];
if(z[i]>=10){
z[i+1]+=(z[i]/10);
z[i]%=10;
}
}
while(z[l3+1]){
++l3;
}
for(int i=l3;i>=1;i--) cout<<z[i];
cout<<endl;
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=510;
char a[maxn],b[maxn];
int x[maxn],y[maxn],z[maxn];
int main(){
memset(z,0,sizeof(z));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
scanf("%s%s",a,b);
int l1=strlen(a),l2=strlen(b);
for(int i=1;i<=l1;i++) x[i]=a[l1-i]-'0';
//for(int i=1;i<=l1;i++) cout<<x[i]<<' ';
//cout<<endl;
for(int i=1;i<=l2;i++) y[i]=b[l2-i]-'0';
//for(int i=1;i<=l2;i++) cout<<y[i]<<' ';
//cout<<endl;
int l3=max(l1,l2),cur=0;
for(int i=1;i<=l3;i++){
z[i]+=x[i]+y[i];
if(z[i]>=10){
z[i+1]+=(z[i]/10);
z[i]%=10;
}
}
while(z[l3+1]){
++l3;
}
for(int i=l3;i>=1;i--) cout<<z[i];
cout<<endl;
return 0;
}
相关文章推荐
- C++: 高精度加法与高精度减法
- 高精度加法模板
- HDU-1047 高精度加法
- 自己写的一个高精度加法,网上有的都太高深,我本身是新手,写的大概新手比较好懂
- 2010年华为上机笔试二(高精度整数加法)
- 大整数加法(高精度)
- 基础练习 高精度加法
- UVA 424 Integer Inquiry 高精度加法
- poj 1503 高精度加法
- HDU 1002 A + B Problem II 高精度加法程序 覆盖大量字符串处理基础 详细注释
- 大数加法 减法 乘法 除法 高精度四则运算
- 3116 高精度练习之加法
- 短短的高精度加法乘法
- 【字符串】【AOJ-437】高精度加法
- 华为OJ——无线OSS-高精度整数加法
- 高精度加法 bignum类实现(hdu 1865)
- wikioi 3116 高精度练习之加法
- 高精度加法
- 1218 -- 高精度加法
- 高精度加法——杭电1002