51NOD 1005 大数加法

#include<iostream>  
#include<string.h>  
#include<stdio.h> 
#include <algorithm> 
using namespace std;  
struct shu{
int x[13000];
int len;
bool zheng;
shu(){
memset(x,0,sizeof(int)*10005);
len=0;
zheng=true;
}
bool iszero();
int judge (shu& b);
};
bool shu::iszero(){
for (int i=0;i<len;++i){
if (x[i]!=0){
return false;
}
}
return true;
}
int judge (shu &a ,shu& b) {
if (a.len>b.len){
return 1;
}
else if (a.len<b.len){
return -1;
}
else if (a.len==b.len){
for (int i=a.len;i>=0;--i){
if (a.x[i]>b.x[i]){
return 1;
}
else if (a.x[i]<b.x[i]){
return -1;
}
}
return 0;
}
}
//chuli
void s(char* a,shu &a1){
int l=strlen(a);
if (a[0]=='-'){
a1.zheng=false;
a1.len=l-1;
for (int i=l-1, j=0;i>=1;--i,++j){
a1.x[j]=(a[i]-'0');
}
}
else{
for (int i=l-1,j=0;i>=0;--i,++j){
a1.x[j]=(a[i]-'0');
}
a1.len=l;
}
}
void Add (shu& a,shu& b,shu& c){
if (a.iszero()&&b.iszero()){
return ;
}
if (a.zheng==false&&b.zheng==true){
swap(a,b);
}
if (a.zheng&&b.zheng){
for (int i=0;i<(a.len>b.len?a.len:b.len);++i){
c.x[i]+=(a.x[i]+b.x[i]);
if (c.x[i]>=10){
c.x[i]%=10;
c.x[i+1]++;
}
}
c.zheng=true;
c.len=(a.len>b.len?a.len:b.len);
    }
else if (a.zheng==true&&b.zheng==false){
if (judge(a,b)==0){
return ;
}
else if (judge(a,b)==1){
//cout<<"here";
for (int i=0;i<a.len;++i){
c.x[i]+=(a.x[i]-b.x[i]);
if (c.x[i]<0){
int j=i+1;
while (a.x[j]==0){++j;}
c.x[j]--;
for (int k=i+1;k<j;++k)
c.x[k]+=9;
c.x[i]+=10;
}
}
c.zheng=true;
}
else if (judge(a,b)==-1){
// cout<<"here";
for (int i=0;i<b.len;++i){
c.x[i]+=(b.x[i]-a.x[i]);
if (c.x[i]<0){
int j=i+1;
while (b.x[j]==0){++j;}
c.x[j]--;
for (int k=i+1;k<j;++k)
c.x[k]+=9;
c.x[i]+=10;
}
}
c.zheng=false;
}
}
    else if (!a.zheng&&!b.zheng){
    for (int i=0;i<(a.len>b.len?a.len:b.len);++i){
c.x[i]+=(a.x[i]+b.x[i]);
if (c.x[i]>=10){
c.x[i]%=10;
c.x[i+1]++;
}
}
c.zheng=false;
}
c.len=(a.len>b.len?a.len:b.len)+1;
}


 int main()  
 {  
    char a[13000],b[13000];
struct shu a1,b1,c1;  
    scanf("%s %s",a,b);  
s(a,a1);s(b,b1);
Add(a1,b1,c1);
bool m=true;
bool v=true;
if (c1.iszero()){
cout<<'0';return 0;
}
if (!c1.zheng){
cout<<'-';
}
for (int i=c1.len;i>=0;--i){
if (c1.x[i]!=0){
m=false;

}

if (!m){
if(c1.x[i]<0)
cout<<-c1.x[i];
else{
cout<<c1.x[i];
}
}
}
    return 0;  

 }  





真的是一边哭一边写的  自己太弱了

以后的目标是天下!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值