一篇文章讲明白MartianAddition

简介: 一篇文章讲明白MartianAddition

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

题目意思:二十进制加法运算

。。。。我的代码:

1 #include

2 #include[span style="color: rgba(0, 0, 255, 1)">string.h>

3 int a【200】,b【200】,c【200】;

4 char x【200】,s【200】,t【200】;

5 int main()

6 {

7 int i,j,k,len1,len2,len;

8 while(scanf("%s%s",x,s)!=EOF)

9 {

10 memset(a,0,sizeof(a));

11 memset(b,0,sizeof(b));

12 memset(c,0,sizeof(c));

13 len1=strlen(x);

14 len2=strlen(s);

15 if(len1>len2)

16 len=len1;

17 else

18 len=len2;

19 i=0;

20 for(j=len1-1; j>=0; j--)

21 {

22 if(x【j】<='9'x【j】>='0')

23 a【i++】=x【j】-'0';

24 else

25 a【i++】=x【j】-87;

26 }

27 i=0;

28 for(j=len2-1; j>=0; j--)

29 {

30 if(s【j】<='9's【j】>='0')

31 b【i++】=s【j】-'0';

32 else

33 b【i++】=s【j】-87;

34 }

35 for(i=0; i

36 {

37 c【i】=c【i】+a【i】+b【i】;

38 if(c【i】>19)

39 {

40 c【i+1】=c【i】/20;

41 c【i】=c【i】%20;

42 }

43 }

44 i=len;

45 while(!c【i】)///000001这种情况,0不能输出,将0跳出

46 {

47 i--;

48 if(i==-1)

49 {

50 printf("0");

51 break;

52 }//代码效果参考:http://www.ezhiqi.com/bx/art_4847.html

53 }

54 k=0;

55 for(j=i; j>=0; j--)

56 {

57 if(c【j】>=0c【j】<=9)

58 t【k++】=c【j】+'0';

59 else

60 t【k++】=c【j】+87;

61 }

62 for(j=0; j

63 printf("%c",t【j】);

64 printf("\n");

65

66 }

67 return 0;

68 }

大佬的代码是这样的:

1 #include///学习:可以将要输出的内容写入一个字符数组之中,输出在字符数组中的位置即可

2 int main()

3 {

4 char a【200】,b【200】,s【21】="0123456789abcdefghij";

5 int c【200】,i,j,k,p,q;

6 while(scanf("%s%s",a,b)!=EOF){

7 memset(c,0,sizeof(c));

8 k=0;

9 p=strlen(a);

10 q=strlen(b);

11 j=q-1;

12 for(i=p-1;i>=0||j>=0;i--){

13 if(i>=0){

14 if(a【i】-'a'

15 c【k】+=a【i】-'a'+10;

16 else

17 c【k】+=a【i】-'0';

18 }

19 if(j>=0){

20 if(b【j】-'a'

21 c【k】+=b【j】-'a'+10;

22 else

23 c【k】+=b【j】-'0';

24 }

25 if(c【k】>=20){

26 c【k】%=20;

27 c【k+1】+=1;

28 }

29 j--;

30 k++;

31 }

32 if(c【k】==0)

33 k-=1;

34 for(i=k;i>=0;i--)

35 printf("%c",s【c【i】】);

36 printf("\n");

37 }//代码效果参考:http://www.ezhiqi.com/bx/art_2931.html

38 return 0;

39 }

学习了

上一篇Java数据结构(六)二叉树入门下一篇Java数据结构(七)堆

本文作者:王陸

本文链接:

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

相关文章
|
XML Java 数据库连接
如何使用MyBatis框架实现对数据库的增删查改?
如何使用MyBatis框架实现对数据库的增删查改?
|
12月前
|
数据采集 搜索推荐
《从数据到转化:游戏地域偏好驱动的精准推送指南》
本文聚焦游戏行业地域化精准推送的核心痛点,提出基于玩家地域偏好的差异化推送体系构建思路。文章从无干扰数据采集与伪偏好筛滤入手,通过三维立体数据模型获取真实需求,再从行为、审美、需求、付费四个维度拆解地域偏好,形成可量化标签。进而搭建“偏好-内容-场景-时段”动态联动机制,定制适配不同地区的推送内容与策略,并依托数据和玩家双轮反馈实现闭环迭代,同时规避过度标签化、静态思维等认知误区。这套方法打破“一刀切”运营模式,助力游戏实现全域用户的长效激活与留存。
523 1
|
安全 数据安全/隐私保护
阿里云账号注册流程图、企业实名认证及问题解答FAQ
阿里云企业账号注册简便,手机号验证码即可完成注册,后续需进行企业实名认证方可使用。支持营业执照上传或法人支付宝扫码认证,多种方式灵活选择。附详细流程与常见问题解答,助力企业快速上云。
|
Web App开发 大数据 应用服务中间件
什么是 HTTP Range请求(范围请求)
HTTP Range 请求是一种非常有用的 HTTP 功能,允许客户端请求资源的特定部分,从而提高传输效率和用户体验。通过合理使用 Range 请求,可以实现断点续传、视频流播放和按需加载等功能。了解并掌握 HTTP Range 请求的工作原理和应用场景,对开发高效的网络应用至关重要。
2830 16
|
Ubuntu 机器人 项目管理
Ubuntu系统更换软件源以及ROS包管理问题解析
以上是针对Ubuntu系统软件源的更换和ROS包管理的关键步骤。务必跟随官方指南,在安装或者配置过程中应答疑解惑,确保每一步操作的准确性。这些操作对于机器人研发人员和爱好者来说是日常任务的一部分,熟练掌握这些技能,能够在机器人编程和项目管理方面提供很大的帮助。
1057 0
|
存储 Java 编译器
Java基础教程(五千字带你快速入门!)(二)
Java基础教程(五千字带你快速入门!)(二)
213 0
|
Java 调度
Java线程生命周期详解
前言 Java中的线程生命周期是多线程开发的核心概念。了解线程的生命周期以及它们如何进行状态转换对于编写有效且无错误的多线程程序至关重要。
648 0
|
机器学习/深度学习 算法
时间复杂度和空间复杂度+剑指offer习题(一)
时间复杂度和空间复杂度+剑指offer习题
[数据科学] 通过基因表达监测进行肿瘤预测
[数据科学] 通过基因表达监测进行肿瘤预测
329 0
[数据科学] 通过基因表达监测进行肿瘤预测

热门文章

最新文章