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;
    }
  • 相关阅读:
    路由交换03-----传输层协议
    Linux结构目录
    Linux RedHat 7 配置本地 YUM源
    HUAWEI,H3C 三层交换机 常用命令
    windows操作系统更改 <远程桌面> 端口号
    SpringBoot 2.x (2):请求和传参
    SpringBoot 2.x (1):手动创建项目与自动创建项目
    Eclipse中使用Maven搭建SSM框架
    基于Spring和Mybatis拦截器实现数据库操作读写分离
    WinServer配置MySQL主从同步
  • 原文地址:https://www.cnblogs.com/stepping/p/5682665.html
Copyright © 2011-2022 走看看