zoukankan      html  css  js  c++  java
  • 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;
    
      }
    
    }
  • 相关阅读:
    java并发计算的几种基本使用示例
    axios、ajax和xhr前端发送测试
    Spring注解
    Android菜鸟教程笔记
    普通二叉树操作
    MyBatis
    mysql的select语句总结与索引使用
    sys.argv的意义[转]
    硬件小白学习之路(1)稳压芯片LM431
    FPGA小白学习之路(6)串口波特率问题的处理
  • 原文地址:https://www.cnblogs.com/malloc/p/2419550.html
Copyright © 2011-2022 走看看