1 条题解

  • 0
    @ 2024-12-24 9:59:27

    C :

    //求两个整数想加的结果,这两个整数都可以用long long 存下,但是它们的结果不一定可以用
    //long long 存下,所以要用大整数来求解
    
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
       char strA[50], strB[50], strC[50];
       int  C[51], lenA, lenB;
    
    
       while(scanf("%s%s", strA, strB) != EOF){
            memset(C, 0, sizeof(C));
          //这一段完成的是A和B都为正数的时候的情况
          if((strA[0]>='0' && strA[0] <= '9') && (strB[0]>='0' && strB[0] <= '9')){
             lenA = strlen(strA);
             lenB = strlen(strB);
    
             int start = 0;
             int ch  = 0;   //ch是用来标记有没有进位的
             int i, j;
             for(i = lenA - 1,j = lenB - 1; i >= 0 && j >= 0; i--, j--) {
                C[start] = ch + strA[i] + strB[j] - '0' - '0';
                if(C[start] > 9) {C[start] = C[start] - 10; ch = 1;}
                else ch = 0;
                start++;
                if(i == 0 && j == 0 && ch == 1)  C[start++] = 1;   //这里需要注意是因为如果A[],B[]同时到了第一个数
             }                                                     //并且此时产生了进位的话,那就需要在数组C[]中表现出来
             if(i > 0) while(i >= 0) {C[start++] = ch + strA[i--] - '0'; ch = 0;}
             if(j > 0) while(j >= 0) {C[start++] = ch + strB[j--] - '0'; ch = 0;}
    
             for(int i = start-1; i >= 0; i--) printf("%d", C[i]);  //把最后的结果输出
             printf("\n");
          }//if
    
          //当A和B都为负数的情况,可以转化为求正数的东西来解
          else if(strA[0] == '-' && strB[0] == '-'){
             lenA = strlen(strA);
             lenB = strlen(strB);
             int start = 0;
             int ch  = 0;
             int i, j;
             for(i = lenA - 1,j = lenB - 1; i >= 1 && j >= 1; i--, j--) {
                C[start] = ch + strA[i] + strB[j] - '0' - '0';
                if(C[start] > 9) {C[start] = C[start] - 10; ch = 1;}
                else ch = 0;
                start++;
                if(i == 1 && j == 1 && ch == 1)  C[start++] = 1;
             }
             if(i > 1) while(i >= 1) {C[start++] = ch + strA[i--] - '0'; ch = 0;}
             if(j > 1) while(j >= 1) {C[start++] = ch + strB[j--] - '0'; ch = 0;}
             printf("-");
             for(int i = start-1; i >= 0; i--) printf("%d", C[i]);
             printf("\n");
             }//else if
    
    
          else{//如果A和B不同时为正数或者同时为负数的话就把它们转化为A为正数B为负数的情况
             if((strB[0]>='0' && strB[0] <= '9') && strA[0] == '-'){
             memset(strC, 0, sizeof(strC));
             strncpy(strC, strA, sizeof(strA));
             strncpy(strA, strB, sizeof(strB));
             strncpy(strB, strC, sizeof(strC));
             }
             lenA = strlen(strA);
             lenB = strlen(strB);
    
    
             if(lenA > lenB - 1){//这种情况就是A一定会比B要大,所以就可以用A去减掉B
                 int start = 0;
                 int ch  = 0;
                 int i, j;
                 for(i = lenA - 1, j = lenB - 1; i >= 0 && j >= 1; i--, j--){
                     C[start] = strA[i] - strB[j] - ch;
                 if(C[start] < 0) {C[start] = 10 + C[start]; ch = 1;}
                 else ch = 0;
                 start++;
                 }
                 while(i >= 0){
                    C[start++] = strA[i--] - '0' - ch;
                    ch = 0;
                }
    
                for(int i = start-1; i >= 0; i--){
                    if(C[i]){
                        while(i>=0){
                            printf("%d", C[i]);
                            i--;
                        }
                        printf("\n");
                        break;
                    }
                }
             }//if
    
    
             else if(lenA == lenB - 1){
                int ok;
                int start = 0;
                if(strncmp(strA, strB+1, lenA) == 0) {printf("0\n"); continue;}  //之前把函数写错了,把strncmp写成了strncpy
                else if(strncmp(strA, strB+1, lenA) > 0) ok = 1;         //strncpy是将后面字符串的前n个复制到前面
                else ok = 0;
                if(ok){  //当A大于B时,就拿A去减掉B
                    int start = 0;
                    int ch  = 0;
                    int i, j;
                    for(i = lenA - 1, j = lenB - 1; i >= 0 && j >= 1; i--, j--){
                       C[start] = strA[i] - strB[j]- ch;
                    if(C[start] < 0) {C[start] = 10 + C[start]; ch = 1;}
                    else ch = 0;
                    start++;
                   }
                   for(int i = start-1; i >= 0; i--){
                     if(C[i]){
                        while(i>=0){
                            printf("%d", C[i]);
                            i--;
                        }
                        printf("\n");
                        break;
                     }
                  }
               }//if
    
               else {
                   int start = 0;
                   int ch = 0;
                   int i, j;
                   for(i = lenA - 1, j = lenB - 1; i >= 0 && j >= 1; i--, j--){
                      C[start] = strB[j] - strA[i] - ch;
                      if(C[start] < 0) {C[start] = 10 + C[start]; ch = 1;}
                      else ch = 0;
                    start++;
                   }
                   printf("-");
                   for(int i = start-1; i >= 0; i--){
                     if(C[i]){
                        while(i>=0){
                            printf("%d", C[i]);
                            i--;
                        }//while
                        printf("\n");
                        break;
                     }//if
                   }//for
               }//else
             }//else if
    
             else {//这种情况就是指A的值的大小必定会小于B
                   int start = 0;
                   int ch = 0;
                   int i, j;
                   for(i = lenA - 1, j = lenB - 1; i >= 0 && j >= 1; i--, j--){
                      C[start] = strB[j] - strA[i] - ch;
                      if(C[start] < 0) {C[start] = 10 + C[start]; ch = 1;}
                      else ch = 0;
                    start++;
                   }
                   while(j >= 1){
                      C[start++] = strB[j--] - '0' - ch;
                      ch = 0;
                   }
                   printf("-");
                   for(int i = start-1; i >= 0; i--){
                     if(C[i]){
                        while(i>=0){
                            printf("%d", C[i]);
                            i--;
                        }//while
                        printf("\n");
                        break;
                     }//if
                   }//for
             }//else
          }//else
    
       }//while(最外层的那个)
       return 0;
    }
    
    

    C++ :

    #include <deque>
    #include <vector>
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <stdio.h>
    using namespace std;
    
    class DividedByZeroException {};
    
    class BigInteger
    {
        private:
            vector<char> digits;
            bool sign;          //  true for positive, false for negitive
            void trim();        //  remove zeros in tail, but if the value is 0, keep only one:)
        public:
            BigInteger(int);    // construct with a int integer
            BigInteger(string&) ;
            BigInteger();
            BigInteger (const BigInteger&);
            BigInteger operator=(const BigInteger& op2);
    
            BigInteger      abs() const;
            BigInteger    pow(int a);
    
            //binary operators
    
            friend BigInteger operator+=(BigInteger&,const BigInteger&);
            friend BigInteger operator-=(BigInteger&,const BigInteger&);
            friend BigInteger operator*=(BigInteger&,const BigInteger&);
            friend BigInteger operator/=(BigInteger&,const BigInteger&) throw(DividedByZeroException);
            friend BigInteger operator%=(BigInteger&,const BigInteger&) throw(DividedByZeroException);
    
            friend BigInteger operator+(const BigInteger&,const BigInteger&);
            friend BigInteger operator-(const BigInteger&,const BigInteger&);
            friend BigInteger operator*(const BigInteger&,const BigInteger&);
            friend BigInteger operator/(const BigInteger&,const BigInteger&) throw(DividedByZeroException);
            friend BigInteger operator%(const BigInteger&,const BigInteger&) throw(DividedByZeroException);
    
    
            //uniary operators
            friend BigInteger operator-(const BigInteger&);   //negative
    
            friend BigInteger operator++(BigInteger&);        //++v
            friend BigInteger operator++(BigInteger&,int);    //v++
            friend BigInteger operator--(BigInteger&);        //--v
            friend BigInteger operator--(BigInteger&,int);    //v--
    
            friend bool operator>(const BigInteger&,const BigInteger&);
            friend bool operator<(const BigInteger&,const BigInteger&);
            friend bool operator==(const BigInteger&,const BigInteger&);
            friend bool operator!=(const BigInteger&,const BigInteger&);
            friend bool operator>=(const BigInteger&,const BigInteger&);
            friend bool operator<=(const BigInteger&,const BigInteger&);
    
            friend ostream& operator<<(ostream&,const BigInteger&);    //print the BigInteger
            friend istream& operator>>(istream&, BigInteger&);         // input the BigInteger
    
    public:
            static const BigInteger ZERO;
            static const BigInteger ONE;
            static const BigInteger TEN;
    };
    
    // BigInteger.cpp
    
     const BigInteger BigInteger::ZERO=BigInteger(0);
     const BigInteger BigInteger::ONE =BigInteger(1);
     const BigInteger BigInteger::TEN =BigInteger(10);
    
    
    BigInteger::BigInteger()
    {
        sign=true;
    }
    
    
    BigInteger::BigInteger(int val){// construct with a int integer
        if (val >= 0)
            sign = true;
        else{
            sign = false;
            val *= (-1);
        }
        do{
            digits.push_back( (char)(val%10) );
            val /= 10;
        } while ( val != 0 );
    }
    
    
    BigInteger::BigInteger(string& def){
        sign=true;
        for ( string::reverse_iterator iter = def.rbegin() ; iter < def.rend();  iter++){
            char ch = (*iter);
            if (iter == def.rend()-1){
                if ( ch == '+' )
                    break;
                if(ch == '-' ){
                    sign = false;
                    break;
                }
            }
            digits.push_back( (char)((*iter) - '0' ) );
        }
        trim();
    }
    
    void BigInteger::trim(){
        vector<char>::reverse_iterator iter = digits.rbegin();
        while(!digits.empty() && (*iter) == 0){
            digits.pop_back();
            iter=digits.rbegin();
        }
        if( digits.size()==0 ){
            sign = true;
            digits.push_back(0);
        }
    }
    
    
    BigInteger::BigInteger(const BigInteger& op2){
        sign = op2.sign;
        digits=op2.digits;
    }
    
    
    BigInteger BigInteger::operator=(const BigInteger& op2){
                digits = op2.digits;
                sign = op2.sign;
                return (*this);
     }
    
    
    BigInteger BigInteger::abs() const {
        if(sign)  return *this;
        else      return -(*this);
    }
    
    BigInteger BigInteger::pow(int a)
    {
        BigInteger res(1);
        for(int i=0; i<a; i++)
            res*=(*this);
        return res;
    }
    
    //binary operators
    BigInteger operator+=(BigInteger& op1,const BigInteger& op2){
        if( op1.sign == op2.sign ){     //只处理相同的符号的情况,异号的情况给-处理
            vector<char>::iterator iter1;
            vector<char>::const_iterator iter2;
            iter1 = op1.digits.begin();
            iter2 = op2.digits.begin();
            char to_add = 0;        //进位
            while ( iter1 != op1.digits.end() && iter2 != op2.digits.end()){
                (*iter1) = (*iter1) + (*iter2) + to_add;
                to_add = ((*iter1) > 9);    // 大于9进一位
                (*iter1) = (*iter1) % 10;
                iter1++; iter2++;
            }
            while ( iter1 != op1.digits.end() ){   //
                (*iter1) = (*iter1) + to_add;
                to_add = ( (*iter1) > 9 );
                (*iter1) %= 10;
                iter1++;
            }
            while ( iter2 != op2.digits.end() ){
                char val = (*iter2) + to_add;
                to_add = (val > 9) ;
                val %= 10;
                op1.digits.push_back(val);
                iter2++;
            }
            if( to_add != 0 )
                op1.digits.push_back(to_add);
            return op1;
        }
        else{
            if (op1.sign)
                return op1 -= (-op2);
            else
                return op1= op2 - (-op1);
        }
    
    }
    
    BigInteger operator-=(BigInteger& op1,const BigInteger& op2){
        if( op1.sign == op2.sign ){     //只处理相同的符号的情况,异号的情况给+处理
            if(op1.sign) {
                if(op1 < op2)  // 2 - 3
                    return  op1=-(op2 - op1);
            }
            else {
                if(-op1 > -op2)  // (-3)-(-2) = -(3 - 2)
                    return op1=-((-op1)-(-op2));
                else             // (-2)-(-3) = 3 - 2
                    return op1= (-op2) - (-op1);
            }
            vector<char>::iterator iter1;
            vector<char>::const_iterator iter2;
            iter1 = op1.digits.begin();
            iter2 = op2.digits.begin();
    
            char to_substract = 0;  //借位
    
            while ( iter1 != op1.digits.end() && iter2 != op2.digits.end()){
                (*iter1) = (*iter1) - (*iter2) - to_substract;
                to_substract = 0;
                if( (*iter1) < 0 ){
                    to_substract=1;
                    (*iter1) += 10;
                }
                iter1++;
                iter2++;
            }
            while ( iter1 != op1.digits.end() ){
                (*iter1) = (*iter1) - to_substract;
                to_substract = 0;
                if( (*iter1) < 0 ){
                    to_substract=1;
                    (*iter1) += 10;
                }
                else break;
                iter1++;
            }
            op1.trim();
            return op1;
        }
        else{
            if (op1 > BigInteger::ZERO)
                return op1 += (-op2);
            else
                return op1 = -(op2 + (-op1));
        }
    }
    BigInteger operator*=(BigInteger& op1,const BigInteger& op2){
        BigInteger result(0);
        if (op1 == BigInteger::ZERO || op2==BigInteger::ZERO)
            result = BigInteger::ZERO;
        else{
            vector<char>::const_iterator iter2 = op2.digits.begin();
            while( iter2 != op2.digits.end() ){
                if(*iter2 != 0){
                    deque<char> temp(op1.digits.begin() , op1.digits.end());
                    char to_add = 0;
                    deque<char>::iterator iter1 = temp.begin();
                    while( iter1 != temp.end() ){
                        (*iter1) *= (*iter2);
                        (*iter1) += to_add;
                        to_add = (*iter1) / 10;
                        (*iter1) %= 10;
                        iter1++;
                    }
                    if( to_add != 0)
                        temp.push_back( to_add );
                    int num_of_zeros = iter2 - op2.digits.begin();
                    while(  num_of_zeros--)
                        temp.push_front(0);
                    BigInteger temp2;
                    temp2.digits.insert( temp2.digits.end() , temp.begin() , temp.end() );
                    temp2.trim();
                    result = result + temp2;
                }
                iter2++;
            }
            result.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) );
        }
        op1 = result;
        return op1;
    }
    
    BigInteger operator/=(BigInteger& op1 , const BigInteger& op2 ) throw(DividedByZeroException) {
        if( op2 == BigInteger::ZERO )
            throw DividedByZeroException();
        BigInteger t1 = op1.abs(), t2 = op2.abs();
        if ( t1 < t2 ){
            op1 = BigInteger::ZERO;
            return op1;
        }
        //现在 t1 > t2 > 0
        //只需将 t1/t2的结果交给result就可以了
        deque<char> temp;
        vector<char>::reverse_iterator iter = t1.digits.rbegin();
    
        BigInteger temp2(0);
        while( iter != t1.digits.rend() ){
            temp2 = temp2 * BigInteger::TEN + BigInteger( (int)(*iter) );
            char s = 0;
            while( temp2 >= t2 ){
                temp2 = temp2 - t2;
                s = s + 1;
            }
            temp.push_front( s );
            iter++;
        }
        op1.digits.clear();
        op1.digits.insert( op1.digits.end() , temp.begin() , temp.end() );
        op1.trim();
        op1.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) );
        return op1;
    }
    
    BigInteger operator%=(BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) {
        return op1 -= ((op1 / op2)*op2);
    }
    
    BigInteger operator+(const BigInteger& op1,const BigInteger& op2){
        BigInteger temp(op1);
        temp += op2;
        return temp;
    }
    BigInteger operator-(const BigInteger& op1,const BigInteger& op2){
        BigInteger temp(op1);
        temp -= op2;
        return temp;
    }
    
    BigInteger operator*(const BigInteger& op1,const BigInteger& op2){
        BigInteger temp(op1);
        temp *= op2;
        return temp;
    
    }
    
    BigInteger operator/(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) {
        BigInteger temp(op1);
        temp /= op2;
        return temp;
    }
    
    BigInteger operator%(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) {
        BigInteger temp(op1);
        temp %= op2;
        return temp;
    }
    
    //uniary operators
    BigInteger operator-(const BigInteger& op){   //negative
        BigInteger temp = BigInteger(op);
        temp.sign = !temp.sign;
        return temp;
    }
    
    BigInteger operator++(BigInteger& op){    //++v
        op += BigInteger::ONE;
        return op;
    }
    
    BigInteger operator++(BigInteger& op,int x){  //v++
        BigInteger temp(op);
        ++op;
        return temp;
    }
    
    BigInteger operator--(BigInteger& op){    //--v
        op -=  BigInteger::ONE;
        return op;
    }
    
    BigInteger operator--(BigInteger& op,int x){  //v--
        BigInteger temp(op);
        --op;
        return temp;
    }
    
    bool operator<(const BigInteger& op1,const BigInteger& op2){
        if( op1.sign != op2.sign )
            return !op1.sign;
        else{
            if(op1.digits.size() != op2.digits.size())
                return (op1.sign && op1.digits.size()<op2.digits.size())
                           || (!op1.sign && op1.digits.size()>op2.digits.size());
            vector<char>::const_reverse_iterator iter1,iter2;
            iter1 = op1.digits.rbegin();iter2 = op2.digits.rbegin();
            while( iter1 != op1.digits.rend() ){
                if(  op1.sign &&  *iter1 < *iter2 ) return true;
                if(  op1.sign &&  *iter1 > *iter2 ) return false;
                if( !op1.sign &&  *iter1 > *iter2 ) return true;
                if( !op1.sign &&  *iter1 < *iter2 ) return false;
                iter1++;
                iter2++;
            }
            return false;
        }
    }
    bool operator==(const BigInteger& op1,const BigInteger& op2){
        if( op1.sign != op2.sign  || op1.digits.size() != op2.digits.size() )
            return false;
        vector<char>::const_iterator iter1,iter2;
        iter1 = op1.digits.begin();
        iter2 = op2.digits.begin();
        while( iter1!= op1.digits.end() ){
            if( *iter1 != *iter2 )  return false;
            iter1++;
            iter2++;
        }
        return true;
    }
    
    bool operator!=(const BigInteger& op1,const BigInteger& op2){
        return !(op1==op2);
    }
    
    bool operator>=(const BigInteger& op1,const BigInteger& op2){
        return (op1>op2) || (op1==op2);
    }
    
    bool operator<=(const BigInteger& op1,const BigInteger& op2){
        return (op1<op2) || (op1==op2);
    }
    
    bool operator>(const BigInteger& op1,const BigInteger& op2){
        return !(op1<=op2);
    }
    
    ostream& operator<<(ostream& stream,const BigInteger& val){    //print the BigInteger
        if (!val.sign)
            stream << "-";
        for ( vector<char>::const_reverse_iterator iter = val.digits.rbegin(); iter != val.digits.rend() ; iter++)
            stream << (char)((*iter) + '0');
        return stream;
    }
    
    istream& operator>>(istream& stream, BigInteger& val){    //Input the BigInteger
        string str;
        stream >> str;
        val=BigInteger(str);
        return stream;
    }
    
    int main()
    {
       // freopen("1.txt","r",stdin);
       // freopen("2.txt","w",stdout);
        string a,b;
        while(cin>>a>>b)
        {
            BigInteger c = BigInteger(a);
            BigInteger d = BigInteger(b);
            cout<< c+d <<endl;
        }
    }
    
    

    Java :

    import java.math.BigInteger;
    import java.util.*;
    
    public class Main{
        public static void main(String args[]){
            Scanner cin=new Scanner(System.in);
            while(cin.hasNext())
            {
                System.out.println(cin.nextBigInteger().add(cin.nextBigInteger()));
            }
        }
    }
    
    • 1

    信息

    ID
    1529
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者