Martian Addition
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<stdio.h>
#include<string.h>
int a[],b[],c[];
char x[],s[],t[];
int main()
{
int i,j,k,len1,len2,len;
while(scanf("%s%s",x,s)!=EOF)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(c,,sizeof(c));
len1=strlen(x);
len2=strlen(s);
if(len1>len2)
len=len1;
else
len=len2;
i=;
for(j=len1-; j>=; j--)
{
if(x[j]<=''&&x[j]>='')
a[i++]=x[j]-'';
else
a[i++]=x[j]-;
}
i=;
for(j=len2-; j>=; j--)
{
if(s[j]<=''&&s[j]>='')
b[i++]=s[j]-'';
else
b[i++]=s[j]-;
}
for(i=; i<len; i++)
{
c[i]=c[i]+a[i]+b[i];
if(c[i]>)
{
c[i+]=c[i]/;
c[i]=c[i]%;
}
}
i=len;
while(!c[i])///000001这种情况,0不能输出,将0跳出
{
i--;
if(i==-)
{
printf("");
break;
}
}
k=;
for(j=i; j>=; j--)
{
if(c[j]>=&&c[j]<=)
t[k++]=c[j]+'';
else
t[k++]=c[j]+;
}
for(j=; j<k; j++)
printf("%c",t[j]);
printf("\n"); }
return ;
}
大佬的代码是这样的:
#include<bits/stdc++.h>///学习:可以将要输出的内容写入一个字符数组之中,输出在字符数组中的位置即可
int main()
{
char a[],b[],s[]="0123456789abcdefghij";
int c[],i,j,k,p,q;
while(scanf("%s%s",a,b)!=EOF){
memset(c,,sizeof(c));
k=;
p=strlen(a);
q=strlen(b);
j=q-;
for(i=p-;i>=||j>=;i--){
if(i>=){
if(a[i]-'a'>=)
c[k]+=a[i]-'a'+;
else
c[k]+=a[i]-'';
}
if(j>=){
if(b[j]-'a'>=)
c[k]+=b[j]-'a'+;
else
c[k]+=b[j]-'';
}
if(c[k]>=){
c[k]%=;
c[k+]+=;
}
j--;
k++;
}
if(c[k]==)
k-=;
for(i=k;i>=;i--)
printf("%c",s[c[i]]);
printf("\n");
}
return ;
}
学习了
Martian Addition的更多相关文章
- [ACM] ZOJ Martian Addition (20进制的两个大数相加)
Martian Addition Time Limit: 2 Seconds Memory Limit: 65536 KB In the 22nd Century, scientists ...
- ZOJ Martian Addition
Description In the 22nd Century, scientists have discovered intelligent residents live on the Mars. ...
- ZOJ Problem Set - 1205 Martian Addition
一道简单题,简单的20进制加减法,我这里代码写的不够优美,还是可以有所改进,不过简单题懒得改了... #include <stdio.h> #include <string.h> ...
- ZOJ 1205 Martian Addition
原题链接 题目大意:大数,20进制的加法计算. 解法:convert函数把字符串转换成数组,add函数把两个大数相加. 参考代码: #include<stdio.h> #include&l ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- iOS 之 SVN提交错误:"XXX" is scheduled for addition, but is missing
今天使用SVN提交项目时,出现了这样的提示:"XXX" is scheduled for addition, but is missing.(无关紧要的东西用XXX代替). 看报错 ...
- POJ 2948 Martian Mining
Martian Mining Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2251 Accepted: 1367 Descri ...
随机推荐
- NPOI读取Excel遇到的坑
NPOI是POI的.NET版本.POI是用Java写成的库,能帮助用户在没有安装Office环境下读取Office2003-2007文件.NPOI在.NET环境下使用,能读写Excel/Word文件. ...
- css 自定义checkbox多选复选框样式
html: <input type="checkbox" id="" value="">菜单1 <input type=& ...
- npm安装包时 --save 和 --save-dev 的区别
以npm 安装 vue为例 1.npm install vue: 会把vue包安装到node_modules目录中: 不会修改package.json文件: 之后运行npm install命令时,不会 ...
- 【转载】jquery实现勾选复选框触发事件给input赋值+回显复选框
引用:https://blog.csdn.net/rui276933335/article/details/45717461 JSP: <td class="as1"> ...
- 【一】调通单机版的thrift-python版本
开发步骤说明 [任务1]调通单机版的thrift-python版本 [任务1]调通单机版的thrift-python版本 安装thrift 创建thrift模块文件并编译 开发python版的clie ...
- 爬虫-爬虫介绍及Scrapy简介
在编写案例之前首先理解几个问题,1:什么是爬虫2:为什么说python是门友好的爬虫语言?3:选用哪种框架编写爬虫程序 一:什么是爬虫? 爬虫 webSpider 也称之为网络蜘蛛,是使用一段编写好的 ...
- 第五节 Go数据结构之队列
一.什么是队列 数据结构里的队列就是模仿现实中的排队.如上图中狗狗排队上厕所,新来的狗狗排到队伍最后,最前面的狗狗撒完尿走开,后面的跟上.可以看出队列有两个特点: (1) 新来的都排在队尾: (2) ...
- 记录:C#监视某个文件的打开记录
首先,先说下为什么要搞这个: 1.首先,我的电脑里有5万左右的目录或文件,用于存放歌曲,数量众多.2.我不一定会用哪种软件听歌(不过也就是几种而已).3.我想在听歌的时候,检测哪首首歌被打开,能获取到 ...
- 滑雪_KEY
滑雪 ( skiing.pas/c/cpp) [题目描述] MM 参加一个滑雪比赛,滑雪场是一个 N×M 的矩形, MM 要从起点( 1, 1)滑到( N,M).矩形中每个单位格子有一个海拔高度值 h ...
- 北京Uber优步司机奖励政策(11月2日~11月8日)
用户组:优步北京人民优步A组(适用于11月2日-11月8日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不 ...