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

    Problem E: 新奇的加法运算

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 2232  Solved: 1352
    [Submit][Status][Web Board]

    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

    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;
    }
    

      

    #include <iostream>
    using namespace std;
    class newInt
    {
    public :
        int num;
        newInt(int t=0):num(t){}
        friend istream &operator>>(istream &is, newInt &p)
        {
            int t; is>>t;
            p.num=t;
            return is;
        }
        friend ostream &operator<<(ostream &os, newInt &p)
        {
            os<<p.num;
            return os;
        }
        newInt &operator + (newInt &p)
        {
            int i=0;
            int max_ , min_;
            if(num>p.num)
            {
                max_=num; min_=p.num;
            }
            else
            {
                max_=p.num; min_=num;
            }
            int sum=0;
            while(max_!=0)
            {
                i++;
                int temp;
                temp=(max_%10+min_%10)%10;
                max_/=10;
                min_/=10;
                for(int j=1; j<i; j++)
                    temp*=10;
                sum+=temp;
            }
            newInt t(sum);
            return t;
        }
    };
    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;
    }
    

      

    作者:7oDo

    仅供参考,请勿抄袭。

    Hang Hang Hang !!!

  • 相关阅读:
    SpringCloudStream实例
    Gateway环境搭建,通过YML文件配置
    Hystrix图形化监控
    Hystrix服务降级
    SpringBootのRedis
    springboot之缓存
    springboot整合JPA
    留言板
    Python 京东口罩监控+抢购
    2019年 自我总结
  • 原文地址:https://www.cnblogs.com/Jie-Fei/p/9125843.html
Copyright © 2011-2022 走看看