• zoj 1205 Martian Addition


    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
     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7  int charToint(char c1){
     8      int result;
     9      if(c1 >= '0' && c1 <= '9')
    10          result = c1 - '0';
    11      else
    12          result = int(c1 - 'a') + 10;
    13      return result;
    14  }
    15 
    16  char intTochar(int a){
    17      char c1;
    18      if(a >= 0 && a <= 9)
    19          c1 = a + '0';
    20      else
    21          c1 = (char)((a - 10) + 'a');
    22      return c1;
    23  }
    24 
    25 int main(){
    26     vector<int> v;
    27     string sa, sb, temp;
    28     int a, b, sum, i, flag;
    29     while(cin >> sa >> sb){
    30         v.clear();
    31         flag = 0;
    32         sum = 0;
    33         int lena = sa.length();
    34         int lenb = sb.length();
    35         if(lena < lenb){
    36             temp = sa;
    37             sa = sb;
    38             sb = temp;
    39         }    
    40         reverse(sa.begin(), sa.end());
    41         reverse(sb.begin(), sb.end());
    42         for(i = 0; (i < lena) && (i < lenb); i++){
    43             a = charToint(sa[i]);
    44             b = charToint(sb[i]);
    45             sum = a + b + flag;
    46             flag = sum / 20;
    47             v.push_back(sum % 20);
    48         }
    49         while(i < lena){
    50             a = charToint(sa[i]);
    51             sum = a + flag;
    52             flag = sum / 20;
    53             v.push_back(sum % 20);
    54             i++;
    55         }
    56         if(flag == 1)
    57             v.push_back(flag);
    58         if(v.size() == 1){//为了排除0 + 0 = 0的情况 
    59                     cout << v[0] << endl;
    60                     continue;
    61         }
    62         i = v.size() - 1;
    63         while(v[i] == 0) {//去掉前导0 
    64                    i--;
    65         }
    66         for(; i >= 0; i--){
    67             cout << intTochar(v[i]);
    68         }
    69         cout << endl;
    70     }
    71     return 0;
    72 }

    代码未ac。。。调试了好久。。。以后再说

     
  • 相关阅读:
    .Net中通过反射技术的应用插件程序的开发入门
    html超链接button
    WCF 跨域 Http绑定
    在ASP.NET MVC中修改默认代码生成/支架模板
    使用Lambda表达式重构委托
    使用公用表表达式(CTE)简化嵌套SQL
    WCF同域动态绑定服务地址
    WCF 跨域TCP绑定
    Silverlight 基础收集
    WCF服务控制台托管方法(不使用配置文件)
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/6527655.html
Copyright © 2020-2023  润新知