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;
    
      }
    
    }
  • 相关阅读:
    SQL清除数据库日志方法
    TFS服务器及服务帐号迁域的处理
    about WBS
    ASP.NET URL Rewrite. URL重写
    查看SQL Server中某数据库下每个表占用的空间大小
    [西安招聘] 微软西安分公司 招聘.NET软件工程师,MOSS开发工程师
    企业微信的数据打通
    常见Post请求与实现
    Python解释器与__pycache__文件夹的生成
    微信开放平台OpenID与UnionID的区别
  • 原文地址:https://www.cnblogs.com/malloc/p/2419550.html
Copyright © 2011-2022 走看看