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. [BZOJ5010][FJOI2017]矩阵填数(状压DP)

    5010: [Fjoi2017]矩阵填数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 45[Submit][Status][ ...

  2. Educational Codeforces Round 43 (Rated for Div. 2) ABCDE

    A. Minimum Binary Number time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. 较有意思的Apple XSS(CVE-2016-7762)漏洞

    文章作者:Avfisher0x00 前言应CVE作者的要求帮忙分析一下这个漏洞,实际上这是一个思路比较有意思的Apple XSS(CVE-2016-7762).漏洞作者确实脑洞比较大也善于尝试和发掘, ...

  4. [HihoCoder1169]猜单词

    题目大意: 给你一个数列,问区间[l,r]内与k最接近的数与k的差是多少. 思路: 将数列中的数和询问的数先从小到大排序, 从小到大枚举每个数,如果是数列上的,就加到线段树中, 如果是询问中的,就在线 ...

  5. bzoj 1503: [NOI2004]郁闷的出纳员 -- 权值线段树

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MB Description OIER公司是一家大型专业化软件公司,有着数以万计的员 ...

  6. MS SQL语句优化

    MS SQL Server查询优化方法查询速度慢的原因很多,常见如下几种 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计 ...

  7. Codeforces Round #303 (Div. 2) C. Woodcutters 贪心

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  8. AMScrollingNavbar框架(自动隐藏导航栏)使用简介

    AMScrollingNavbar框架是一个可以上拉隐藏导航栏和下拉显示导航栏的框架,这个开源框架的调用也很简单,本章节就给大家介绍一下这个框架的用法. 一.下载及导入框架 AMScrollingNa ...

  9. CMSIS-SVD 系统视图说明

    CMSIS 到底是什么? 先来看看ARM公司对CMSIS的定义: ARM® Cortex™ 微控制器软件接口标准 (CMSIS) 是 Cortex-M 处理器系列的与供应商无关的硬件抽象层. CMSI ...

  10. 《UNIX环境高级编程》笔记--errno是否是线程安全的?

    当UNIX函数出错时,常常返回一个负数,而且整形变量errno通常被设置为含有附加信息的一个值,例如,open函数如成功,返回 一个非负文件描述符,如果出错就返回-1,在open出错时,有大约15种不 ...