• ZOJ Problem Set–1205 Martian Addition


    Time Limit: 2 Seconds      Memory Limit: 65536 KB


      In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time. This year they also invite people on Earth to join the contest.
      As the only delegate of Earth, you're sent to Mars to demonstrate the power of mankind. Fortunately you have taken your laptop computer with you which can help you do the job quickly. Now the remaining problem is only to write a short program to calculate the sum of 2 given numbers. However, before you begin to program, you remember that the Martians use a 20-based number system as they usually have 20 fingers.
    Input:
    You're given several pairs of Martian numbers, each number on a line.
    Martian number consists of digits from 0 to 9, and lower case letters from a to j (lower case letters starting from a to present 10, 11, ..., 19).
    The length of the given number is never greater than 100.
    Output:
    For each pair of numbers, write the sum of the 2 numbers in a single line.
    Sample Input:
    1234567890
    abcdefghij
    99999jjjjj
    9999900001

    Sample Output:
    bdfi02467j
    iiiij00000


    Source: Zhejiang University Local Contest 2002, Preliminary

    #include<iostream>
    
    #include<string>
    
    #include<algorithm>
    
    using namespace std;
    
    string MAdd(string& a, string& b)
    
    {
    
      int carry = 0;
    
      int a0, b0, c0;
    
      int i = 0;
    
      for(; i < a.length();i++)
    
      {
    
        if( i >= b.length())
    
          b0 = 0;
    
        else
    
        {
    
          if(b[i] >= 'a')
    
            b0 = b[i] - 'a' + 10;
    
          else
    
            b0 = b[i] - '0';
    
        }
    
        if(a[i] >= 'a')
    
          a0 = a[i] - 'a' + 10;
    
        else
    
          a0 = a[i] - '0';
    
        c0 = a0 + b0 + carry;
    
        if(c0 >= 30)
    
        {
    
          a[i] = c0 - 30 + 'a';
    
          carry = 1;
    
        }
    
        else if(c0 >= 20 && c0 < 30)
    
        {
    
          a[i] = c0 - 20 + '0';
    
          carry = 1;
    
        }
    
        else if(c0 >= 10 && c0 < 20)
    
        {
    
          a[i] = c0 - 10 + 'a';
    
          carry = 0;
    
        }
    
        else
    
        {
    
          a[i] = c0 + '0';
    
          carry = 0;
    
        }
    
      }
    
      if(carry == 1)
    
      {
    
        reverse(a.begin(), a.end());
    
        return "1" + a;
    
      }
    
      else
    
      {
    
        reverse(a.begin(), a.end());
    
        return a;    
    
      }
    
    }
    
    int main()
    
    {
    
      string a, b;
    
      while(cin>>a>>b)
    
      {
    
        reverse(a.begin(), a.end());
    
        reverse(b.begin(), b.end());
    
        cout<<(a.length() > b.length() ? MAdd(a, b): MAdd(b, a))<<endl;
    
      }
    
    }
  • 相关阅读:
    ios -- 教你如何轻松学习Swift语法(一)
    collectionView,tableView的细节处理
    主流界面搭建原理(类似百思不得姐主界面)
    ios--时间格式化(cell业务逻辑处理)
    test
    Mac下安装Matlab R2015b
    最大奇约数
    编码问题
    最优二叉查找树
    二维数组和二级指针
  • 原文地址:https://www.cnblogs.com/malloc/p/2419550.html
Copyright © 2020-2023  润新知