zoukankan      html  css  js  c++  java
  • ZOJ Martian Addition

    ZOJ Martian Addition

    Time Limit: 2 Sec  Memory Limit: 128 MB

    Submit: 30  Solved: 14

    [Submit][Status][Web Board]

    Description

     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

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[105];
    int l1,l2;
    char ch1[105],ch2[105];
    int work(char ch)
    {
        if (ch>='0' && ch<='9') return ch-'0';
        if (ch>='a') return ch-'a'+10;
    }
    char solve(int k)
    {
        if (k<=9) return (char)(k+48);
         else return (char)(k+97-10);
    }
    int main()
    {
        while(~scanf("%s",&ch1))
        {
            l1=strlen(ch1);
            scanf("%s",&ch2);
            l2=strlen(ch2);
            memset(a,0,sizeof(a));
            if (l1<l2) {swap(l1,l2);swap(ch1,ch2);}
            int k=l1-l2;
            for(int i=l1;i>0;i--) a[i]=work(ch1[i-1]);
            for(int i=l2-1;i>=0;i--)
            {
                int y=work(ch2[i]);
                a[i+k+1]+=y;
            }
    
            for(int i=l1;i>0;i--)
            {
                a[i-1]+=a[i]/20;
                a[i]=a[i]%20;
            }
           // for(int i=0;i<=l1;i++) printf("%d ",a[i]);
            if (a[0]>0) printf("%c",solve(a[0]));
            for(int i=1;i<=l1;i++) printf("%c",solve(a[i]) );
           // for(int i=1;i<=l1;i++) printf("%d",a[i]);
              printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    [转] 一文读懂 HTTP/2 特性
    设置VS2019 支持C++17标准
    switch case 字符串表达式支持
    在Fabric实现类似Uniswap的去中心化交易所
    数据上链的原则与方式
    2.4g无线私有协议透传方案特色梳理
    无线数字麦克风解决方案小结
    高保真的音频编解码器模块及方案解析
    基于wifi的音频采集及处理解决方案小结
    基于智能降噪的助听器解决方案解析
  • 原文地址:https://www.cnblogs.com/stepping/p/5682665.html
Copyright © 2011-2022 走看看