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

解题思路:

求两个20进制的数的和,用数组模拟相加就能够。

代码:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
using namespace std;
map<int,char>change; char num[105];
int add1[105];
int add2[105];
int ans[105];
int jin[105]; void prepare()
{
change[0]='0';change[1]='1';change[2]='2';change[3]='3';
change[4]='4';change[5]='5';change[6]='6';change[7]='7';
change[8]='8';change[9]='9';change[10]='a';change[11]='b';
change[12]='c';change[13]='d';change[14]='e';change[15]='f';
change[16]='g';change[17]='h';change[18]='i';change[19]='j';
} int main()
{
prepare();
while(cin>>num)
{
memset(add1,0,sizeof(add1));
memset(add2,0,sizeof(add2));
memset(jin,0,sizeof(jin));
int len1=strlen(num);
int t=0;
for(int i=len1-1;i>=0;--i)
{
if(!(num[i]>='0'&&num[i]<='9'))
add1[t++]=num[i]-87;
else
add1[t++]=num[i]-'0';
}
cin>>num;
int len2=strlen(num);
t=0;
for(int i=len2-1;i>=0;--i)
{
if(!(num[i]>='0'&&num[i]<='9'))
add2[t++]=num[i]-87;
else
add2[t++]=num[i]-'0';
}
//输入处理完成
//for(int i=0;i<len2;i++)
// cout<<add2[i];
//cout<<endl; int len=max(len1,len2);
for(int i=0;i<=len;i++)
{
int temp=add1[i]+add2[i]+jin[i];
if(temp>=40)
{
ans[i]=temp-40;
jin[i+1]=2;
}
else if(temp>=20)
{
ans[i]=temp-20;
jin[i+1]=1;
}
else
ans[i]=temp;
}
if(ans[len])
cout<<change[ans[len]];
for(int i=len-1;i>=0;i--)
cout<<change[ans[i]];
cout<<endl;
}
return 0;
}

[ACM] ZOJ Martian Addition (20进制的两个大数相加)的更多相关文章

  1. ZOJ Martian Addition

    Description In the 22nd Century, scientists have discovered intelligent residents live on the Mars. ...

  2. ZOJ Problem Set - 1205 Martian Addition

    一道简单题,简单的20进制加减法,我这里代码写的不够优美,还是可以有所改进,不过简单题懒得改了... #include <stdio.h> #include <string.h> ...

  3. ZOJ 1205 Martian Addition

    原题链接 题目大意:大数,20进制的加法计算. 解法:convert函数把字符串转换成数组,add函数把两个大数相加. 参考代码: #include<stdio.h> #include&l ...

  4. PAT1027 Colors in Mars (20分) 10进制转13进制

    题目 People in Mars represent the colors in their computers in a similar way as the Earth people. That ...

  5. ASCII和16进制

    所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...

  6. python进制转换及变量

    .编译型语言和解释型语言的区别? 答: () 编译型语言:是将所有源码编译完成二进制后才进行输出,运行快,执行效率高,时间周期长. ()解释型语言:逐行编译输出,执行效率相对慢,开发效率高, .Pyt ...

  7. ASCII码与16进制的互相转换(表)

    所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...

  8. 数组中hashCode就是内存地址,以及汉字幻化为16进制或10进制

    int[] arr4={1,2,3,4,5}; System.out.println("arr4: "+arr4); System.out.println("arr4.h ...

  9. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

随机推荐

  1. 安卓 listView 优化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_m ...

  2. 【UOJ #204】【APIO 2016】Boat

    http://uoj.ac/problem/204 肯定要离散化的,先离散化出\(O(n)\)个取值区间. 设\(f(i,j)\)表示第\(i\)所学校派出的划艇数量在\(j\)区间中. \(f(i, ...

  3. UVA11019 Martix Matcher --- AC自动机

    UVA11019 Martix Matcher 题目描述: 给定一个\(n*m\)的文本串 问一个\(x*y\)的模式串出现的次数 AC自动机的奇妙使用 将\(x*y\)的模式串拆分成x个串,当x个串 ...

  4. Android UI设计规范之常用单位

    px :全称--pixel .像素.例如,480*800的屏幕在横向有320个象素,在纵向有480个象素. 屏幕的分辨率 : 屏幕的长宽方向上像素点的数量. dp(dip) : 全称--Density ...

  5. hdu 3642 体积并

    题意:求三个矩形体积的并 链接:点我 枚举z #include<stdio.h> #include<iostream> #include<stdlib.h> #in ...

  6. struts2漏洞S2-046修复解决方案

    项目验收通过半年之后, 甲方找了一些网络砖家用工具扫描我司做的社保卡申领系统, 找到了struts2漏洞S2-046, 真是服了, 只知道struts2有bug, 现在才知道它漏洞. 砖家们给出了修复 ...

  7. Codeforces Round #300 D. Weird Chess 水题

    D. Weird Chess Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/proble ...

  8. intellj远程调试设置

    场景:本地intelllij想远程调试服务器A,服务器A上起得是resin服务 步骤: 1.登陆服务器A,给resin添加启动参数. 方法一:修改resin.xml,修改完后重启resin服务器 在r ...

  9. Sencha Touch开发环境搭建及ext插件Spket安装

    第一步:JAVA SDK(JDK)的安装 以去问百度下载JDK,也可以到官方下载JDK. 下载地址: http://www.oracle.com/technetwork/java/javase/dow ...

  10. asp动态数组

    本文所说的 ASP 数组是指在 ASP 中以默认语言 VBScript 为语言的数组. 样例: Dim   MyArray()        for   i   =   0   to   10    ...