0
点赞
收藏
分享

微信扫一扫

Problem E: 新奇的加法运算

写心之所想 2023-05-25 阅读 97


Home

Web Board

ProblemSet

Standing

Status

Statistics


Problem E: 新奇的加法运算


Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 1117  

Solved: 685

[Submit][Status][Web Board]


Description



定义类newInt,包括:

1. int类型的数据成员。

2. 重载运算符“+”。计算规则为:将A、B对应位置上的数字相加,只保留个位数作为结果的对应位置上的数字。比如:876 + 543 = 319。注意:该运算不改变两个操作数的值。

3. 重载输入和输出运算符,用于输入和输出对象的属性值。

4. 无参构造函数和带参构造函数。



Input



第1行N>0,表示测试用例数量。

每个测试用例包括2个非负整数,用空格隔开。



Output



见样例。



Sample Input



4876 543999 99999 1999199 88



Sample Output



876 + 543 = 319999 + 9999 = 98889 + 1999 = 1998199 + 88 = 177



HINT



 不能使用string、char等字符或字符串类型。



Append Code



append.cc,


[ Submit][Status][Web Board]


한국어<  中文 فارسی English ไทย All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin


#include <iostream>
    #include <cstdio>
    using namespace std;
  
    class newInt
    {
    private:
        int x;
    public:
        newInt():x(0){}
        newInt(int xx):x(xx){}
        ~newInt(){}
        friend newInt operator + (const newInt &m,const newInt &n)
        {
            newInt t;
            int arr[100];
            int arr2[100];
            int arr3[100];
            for(int i=0;i<100;i++)
                arr[i]=0;
            for(int i=0;i<100;i++)
                arr2[i]=0;
                for(int i=0;i<100;i++)
                arr3[i]=0;
  
            int flag=0;
            int a=0;
            int b =0;
            int xx = m.x;
            int yy = n.x;
            while(xx>0)
            {
                arr[a++] = xx%10;
                xx =xx/10;
                flag++;
            }
            int flag2=0;
            while(yy>0)
            {
                arr2[b++] = yy%10;
                yy /= 10;
                flag2++;
            }
            flag = max(flag,flag2);
            for(int i=0;i<flag;i++)
            {
                if(arr[i]+arr2[i]<10)
                    arr3[i] = arr[i]+arr2[i];
                    else arr3[i] = arr[i]+arr2[i]-10;
            }
            int sum=0;
            for(int i=flag;i>=0;i--)
            {
                sum=10*sum;
                sum += arr3[i];
            }
            t.x= sum;
            return t.x;
        }
       friend ostream &operator << (ostream &os,newInt &t)
        {
            os<<t.x;
            return os;
        }
       friend istream &operator >>(istream &is,newInt &t)
        {
            is>>t.x;
            return is;
        }
    };
int main()
{
    int cases;
    newInt a, b, c;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>a>>b;
        c = a + b;
        cout<<a<<" + "<<b<<" = "<<c<<endl;
    }
    return 0;
}



举报

相关推荐

0 条评论