zoukankan      html  css  js  c++  java
  • Problem B: 新奇的加法运算

    Description

    定义类newInt,包括:

    1. int类型的数据成员。

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

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

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

    Input

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

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

    Output

    见样例。

    Sample Input

    4
    876 543
    999 9999
    9 1999
    199 88

    Sample Output

    876 + 543 = 319
    999 + 9999 = 9888
    9 + 1999 = 1998
    199 + 88 = 177

    HINT

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

    Append Code

    #include<iostream>
    using namespace std;
    int amazing(int a,int b)
    {
        int n,n1,n2,s[100],i=0,sum=0,flag;
        while(a>0||b>0)
        {
            n1=a%10;
            n2=b%10;
            n=n1+n2;
            if(n>=10)
                n-=10;
            s[i]=n;
            i++;
            a/=10;
            b/=10;
        }
        s[i]=0;
        for(;i>=0;i--)
        {
            sum*=10;
            sum+=s[i];
        }
        return sum;
     
    }
    class newInt
    {
    private:
        int a;
    public:
        newInt(int x=0):a(x) {}
        friend ostream& operator<<(ostream &os,const newInt& q)
        {
            os<<q.a;
            return os;
        }
        friend istream& operator>>(istream &is,newInt& q)
        {
            is>>q.a;
            return is;
        }
        newInt operator+(newInt d)
        {
            return amazing(a,d.a);
        }
    };
    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;
    }
  • 相关阅读:
    HDU 4278 Faulty Odometer 8进制转10进制
    hdu 4740 The Donkey of Gui Zhou bfs
    hdu 4739 Zhuge Liang's Mines 随机化
    hdu 4738 Caocao's Bridges tarjan
    Codeforces Gym 100187M M. Heaviside Function two pointer
    codeforces Gym 100187L L. Ministry of Truth 水题
    Codeforces Gym 100187K K. Perpetuum Mobile 构造
    codeforces Gym 100187J J. Deck Shuffling dfs
    codeforces Gym 100187H H. Mysterious Photos 水题
    windows服务名称不是单个单词的如何启动?
  • 原文地址:https://www.cnblogs.com/TogetherLaugh/p/6595785.html
Copyright © 2011-2022 走看看