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;
    
      }
    
    }
  • 相关阅读:
    Silverlight MIS管理系统
    微软计划将Silverlight移植到机顶盒
    Silverlight VS2010下的RIA开发活动整站
    【原创】展望Silverlight 5.0新版本更新与发展
    Silverlight DeepGrid控件
    js的嵌套函数与闭包函数
    (学)仔细想想、其实有更好的解决办法
    (转变ing)工作内容变化ing
    (转)动态调用WCF
    IIS 异常 “System.OutOfMemoryException”、“存储空间不足,无法完成此操作。”
  • 原文地址:https://www.cnblogs.com/malloc/p/2419550.html
Copyright © 2011-2022 走看看