1001. Problem A+B (Big Integer)

JacobLiu

Don’t you know there are languages called Python or Java?

10175102262 LarsPendragon

C语言(C班)第二次月考F题题解:
(写的有点笨,将就看吧)

#include <stdio.h>
int main()
{
    char a[550], b[550], c, *p1, *p2;
    while(scanf("%s %s",a,b)!=EOF)
    {
        int i, l1=0, l2=0;
        p1=a;
        p2=b;
        while(*p1) {p1++; l1++;}//计算数字长度
        while(*p2) {p2++; l2++;} 
        for(i=0; i<l1/2; i++) {c=a[i]; a[i]=a[l1-i-1]; a[l1-i-1]=c;}//反转数字模拟列竖式计算加法
        for(i=0; i<l2/2; i++) {c=b[i]; b[i]=b[l2-i-1]; b[l2-i-1]=c;}
        for(i=l1; i<550; i++) a[i]='0';
        for(i=l2; i<550; i++) b[i]='0';
        for(i=0; i<540; i++)
        {
            a[i]+=b[i]-'0';//相加
            a[i+1]+=(a[i]-'0')/10;//进位
            a[i]=(a[i]-'0')%10+'0';//保留一位数字
        }
        for(i=540; i>=0; i--) if(a[i]!='0') break;//去除数字前无意义的0
        for(; i>=0; i--) printf("%c",a[i]);
        printf("\n");
    }
    return 0;
}
10162100310

your code is inspiring. it helps me know some of the mistakes that i made.

Andrew-Malcom

include

using namespace std;
class BigInteger{
private:
        char str[10050];
        int num[10050]={0};
public:
        BigInteger(){}
        ~BigInteger(){}
        BigInteger(char *s){
                memset(str,'0',sizeof(str));
                memset(num,0,sizeof(num));
                int len=0;
                for(int i=0;;i++){
                        if(*(s+i)=='\0'){
                                len=i;break;
                        }
                }
                for(int i=0;i<len;i++){
                        str[i]=*(s+len-i-1);
                }
        }
        int getlen() const{
                int len=0;
                for(int i=0;i<10050;i++){
                        if(*(str+i)=='0'){
                                len=i;break;
                        }
                }
                return len;
        }
        void show() const {
                int pos=0;
                for(int i=10050;i>=0;i--){
                        if(num[i]!=0){
                                pos=i;break;
                        }
                }
                for(int i=pos;i>=0;i--) cout<<num[i];
        }
        BigInteger operator+(const BigInteger &T) const{
                BigInteger output;
                for(int i=0;i<10050;i++){
                        if(T.str[i]-'0'+str[i]-'0'+output.num[i]<=9){
                                output.num[i]+=T.str[i]-'0'+str[i]-'0';
                        }
                        else{
                                output.num[i]+=T.str[i]-'0'+str[i]-'0'-10;
                                output.num[i+1]++;
                        }
                }
                return output;
        }
};
int main()
{
        char s1[10050],s2[10050];
        while(cin>>s1>>s2)
        {
                cin.get();
                BigInteger A(s1),B(s2),C;
                C=A+B;
                C.show();
                cout<<endl;
        }
}
公仲粱

include

include

include

using namespace std;

int main()
{
string string1, string2;
while(cin >> string1 >> string2){
string string3;
int maxlength, minlength;
bool oversize = false;

//获得两个数字的长度并将位数小的数字前面补0
if(string1.length() <= string2.length()){
    maxlength = string2.length();
    minlength = string1.length();
    string1.insert(0, maxlength-minlength, '0');
    }
else{
    minlength = string2.length();
    maxlength = string1.length();
    string2.insert(0, maxlength-minlength, '0');
    }

for(int j = maxlength-1; j >= 0 ; j--){
    stringstream ss1, ss2, ss3, ss4;
    int a,b;
    string c,d;
    ss1 << string1[j];  ss1 >> a;  ss1.clear();
    ss2 << string2[j];  ss2 >> b;  ss2.clear();

    //如果上一位oversize(和大于10),这一次基础数加1 
    if(oversize){
        a += 1;
        oversize = false;
    }

    ss3 << a+b-10;      ss3 >> c;  ss3.clear();
    ss4 << a+b;         ss4 >> d;  ss4.clear();

    //如果和大于10在结果字符串前插入c=a+b-10并标记此次oversize
    if(a + b >= 10){
        string3.insert(0, c);
        oversize = true;
    }else
        string3.insert(0, d);

     //如果最后一次相加还oversize, 在总和前插入1进位
    if(oversize && j == 0) string3.insert(0,1,'1');
}
cout << string3 << endl;

}
return 0;

Saitama
#include <bits/stdc++.h>

using namespace std;

int main()
{
    string a, b;
    while(cin >> a >> b)
    {
        a = '0' + a;
        b = '0' + b;
        string t;
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        if(a.size() >= b.size())
        {
            for(int i = 0; i < b.size(); i++)
            {
                a[i] = a[i] - '0' + b[i];
                if(a[i] > '9')
                {
                    a[i] -= 10;
                    a[i + 1]++;
                }
            }
            t = a;
        }
        else
        {
            for(int i = 0; i < a.size(); i++)
            {
                b[i] = b[i] - '0' + a[i];
                if(b[i] > '9')
                {
                    b[i] -= 10;
                    b[i + 1]++;
                }
            }
            t = b;
        }
        for(int i = 0; i < t.size(); i++)
        {
            if(t[i] > '9')
            {
                t[i] -= 10;
                t[i + 1]++;
            }

        }
        reverse(t.begin(), t.end());
        if(t[0] == '0')
            t.erase(0, 1);
        cout << t << endl;
    }
    return 0;
}

重写基本题

aiden
#include <iostream>
#include <algorithm>
#include <string>
#include <array>
#define MAX_LEN 500
using namespace std;

void stringToint(const string &str, array<int,MAX_LEN> &arr, string::size_type length)
{
    auto arr_idx = length - 1;
    for (auto i = 0; i < length; ++i)
    {
        arr[arr_idx] = str[i] - '0';
        --arr_idx;
    }
}

int main()
{   
    string a, b;
    while (cin >> a >> b)
    {
        array<int, MAX_LEN> c{}, d{}, e{};
        stringToint(a, c, a.size());
        stringToint(b, d, b.size());
        bool carry = false;
        auto upper = max(a.size(), b.size());
        for (auto i = 0; i < upper; ++i)
        {
            e[i] += c[i] + d[i];
            if (e[i] >= 10)
            {
                if (i == upper - 1)
                    carry = true;
                ++e[i + 1];
                e[i] %= 10;
            }
        }
        if (carry)
            cout << "1";
        auto iter = e.begin();
        advance(iter, upper - 1);
        while (true)
        {
            cout << *iter;
            if (iter == e.begin())
                break;
            --iter;
        }
        cout << endl;
    }

    return 0;
}
lnu_cxn

刚用python
while True:
try:
a,b=map(int,input().split())
print(a+b)
except:
break

chamodsd

include

include

using namespace std;

void plus_all(string,string,int);
int gain(void);

int main(void) {
gain();

return 0;

}

int gain(void) {

string x,y;
int bits_x,bits_y;

if (cin >> x >> y) {

    bits_x = x.size(); //x的位数-2是字符串末尾的"\0"??
    bits_y = y.size(); //y的位数


    if (bits_y > bits_x) {
        while (bits_x < bits_y){
            x = "0"+ x;
            bits_x++;
        }
    }
    else if (bits_x > bits_y) {
        while (bits_x > bits_y){
            y = "0"+ y;
            bits_y++;
        }
    }
    else {}

    plus_all(x,y,bits_x); //计算x,y的和并输出

    gain(); //判断是否还有输入

}
//获得2个位数相同的字符串



return 0;

}

void plus_all(string x,string y,int bits_x) {
string res = “”;
int i,num;
int extra = 0;
for (i=bits_x; i>=1; i–) {

    num = int(x[i-1]) + int(y[i-1]) - 48 + extra;

    if (num > 57 ){
        extra = 1;
        res = char(num - 10) + res;
    }
    else {
        extra = 0;
        res = char(num) + res;
    }

}
cout << res << endl;

}
为啥不对= =。。。

10175102141

include

include

int main()
{
char x[100000],y[100000];
int a,b,c,i,j;
while((scanf(“%s%s”,x,y))!=EOF)
{
int sum[100000]={0};
a=strlen(x);
b=strlen(y);
if(a>b)
{ for(i=0;i=0;i–,j–)
{
sum[j]=x[j]+y[i]-‘0’-‘0’;
}
for(i=a-1;i>=1;i–)
if(sum[i]>=10) {sum[i-1]++;sum[i]-=10;}
for(i=0;i=0;i--,j--) { sum[j]=x[i]+y[j]-'0'-'0'; } for(i=b-1;i>=1;i--) if(sum[i]>=10) {sum[i-1]++;sum[i]-=10;}
for(i=0;i<b;i++)
printf(“%d”,sum[i]);

}
   if(a==b)
    {
        for(i=b-1;i>=0;i--)
        {
            sum[i]=sum[i]+x[i]+y[i]-'0'-'0';

            if(sum[i]>=10&&i!=0)   {sum[i-1]++;sum[i]-=10;}

        }
          for(i=0;i<a;i++)
            printf("%d",sum[i]);

}
printf(“\n”);
}}

10162100217

include

include

int main()
{
int a[510]={0},b[510]={0},i,j,d,e,t,l;
char c[510],f[510];
while(scanf(“%s %s”,c,f)!=EOF)
{
i=0;j=0;
i=strlen(c);
j=strlen(f);
if(i>=j)
{
for(l=0;l<i;l++)
{
a[l+1]=c[l]-‘0’;
}
for(l=0;l<j;l++)
{
b[l+1]=f[l]-‘0’;
}
}
else
{
for(l=0;l<j;l++)
{
a[l+1]=f[l]-‘0’;
}
for(l=0;l=1;j–)
{
a[i]=a[i]+b[j]+d;
d=a[i]/10;
a[i]=a[i]%10;
i–;
}
while(d==1)
{
d=(a[i]+d)/10;
a[i]=(a[i]+d)%10;
i–;
}
if(a[0]==1)
printf(“1”);
for(i=1;i<=e;i++)
printf(“%d”,a[i]);
printf(“\n”);
}
return 0;
}

你当前正在回复 博客/题目
存在问题!