51Nod1005(大数加法)

H:
给出2个大整数A,B,计算A+B的结果。
Input 第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Sample Input
68932147586
468711654886
Sample Output
537643802472

最初我只写了大数的加法,提交上去 wa回来一看 发现AB还可能是负数。

大数加法代码:

#include <iostream>
#include <cstring>
#include <cmath>
#define maxn 10001
using namespace std;
int  a[maxn],b[maxn],sum[maxn];
int main()
{
    memset (a,0,sizeof(a));
    memset (b,0,sizeof(b));
    memset(sum,0,sizeof(sum));
    string wyha,wyhb;
    cin>>wyha>>wyhb;
    for(int i=0;i<wyha.length();i++)
        a[i]=wyha[wyha.length()-i-1]-'0';
    for(int i=0;i<wyhb.length();i++)
        b[i]=wyhb[wyhb.length()-i-1]-'0';
    int wyhmax=max(wyha.length(),wyhb.length());
    for(int i=0;i<wyhmax;i++)
        sum[i]=a[i]+b[i];
    for(int i=0;i<wyhmax;i++)
    {
        if(sum[i]>=10&&i!=wyhmax-1)
            {sum[i]=sum[i]%10;
             sum[i+1]++;
            }
    }
    while(sum[wyhmax-1]==0)
        wyhmax--;
        for(int i=wyhmax-1;i>=0;i--)
            cout<<sum[i];
    return 0;
}

以上代码只含大数加法,即不接受存在负数的加法。    但是我改的时候死活改不对,看来还是基础不扎实,然后   

看了学长怎么写的呗。。。

正解:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
const int N = 1e5+10;
char str1[N], str2[N], str3[N];
char * add(char *s, char *ss) {
    memset(str3, 0, sizeof(str3));
    int len1 = strlen(s)-1;
    int len2 = strlen(ss)-1;
    int len3 = 0, x, y, flag = 0;
    while(len1 >= 0 || len2 >= 0) {
        if(len1 >= 0) x = s[len1] - '0';
        else x = 0;
        if(len2 >= 0) y = ss[len2] - '0';
        else y = 0;
        str3[len3++] = (x+y+flag)%10 + '0';
        flag = (x+y+flag)/10;
        len2--;len1--;
    }
 if(flag) str3[len3++] = '1';//flag最高位??
    for(int i = 0; i < len3/2; i ++) {
        swap(str3[i],str3[len3-i-1]);
    }
    return str3;
}

char *sub(char *s, char *ss) {
    memset(str3, 0, sizeof(str3));
    int len1 = strlen(s)-1, len2 = strlen(ss)-1;
    int flag = 1, len3 = 0, x, y, cnt = 0;
    if(strcmp(s,ss) == 0){
        str3[len3++] = '0';
        return str3;
    }
    if(len1 < len2 || (len1==len2&&strcmp(s,ss)<0)) {
        swap(len1,len2);
        swap(s,ss);
        flag = 0;
    }
    while(len1 >= 0 || len2 >= 0) {
        if(len1 >= 0) x = s[len1--] - '0';
        else x = 0;
        if(len2 >= 0) y = ss[len2--] - '0';
        else y = 0;
        int ans = x-y+cnt;
        if(ans < 0) {
            str3[len3++] = ans+10+'0';
            cnt = -1;
        } else {
            str3[len3++] = ans+'0';
            cnt = 0;
        }
    }
 while(str3[len3-1] == '0') {
  str3[--len3] = '\0';
 }
    if(!flag) str3[len3++] = '-';
    for(int i = 0; i < len3/2; i ++) swap(str3[i], str3[len3-i-1]);
    return str3;
}
int main() {
 cin >> str1 >> str2;
 if(str1[0] != '-' && str2[0] != '-') {
  cout << add(str1,str2) << endl;
 } else if(str1[0] == '-' && str2[0] != '-') {
  cout << sub(str2,str1+1) << endl;
 } else if(str1[0] != '-' && str2[0] == '-') {
  cout << sub(str1, str2+1);
 } else {
  cout << "-" << add(str1+1,str2+1) << endl;
 }
 return 0;
}

思路就是写出两个子函数  一个加一个减,然后在main函数中看情况调用

这个减的子函数我还得在仔细看看。。。。

猜你喜欢

转载自blog.csdn.net/consine926/article/details/80964982