最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧。

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2

1 2

112233445566778899 998877665544332211

Sample Output

Case 1:

1 + 2 = 3

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110

程序思路:

  1. 使用scanf函数以字符串格式接收两个数,分别存在两个数组中,数组中每一个成员对应数的一位(0-9);
  2. 由于数组中存放的是每个数字对应的ASCII码,所以将其减去0得到十进制的数值;
  3. 对比连个数组的长度,如果长度不等,就补0,使其在做加法时位数能够对齐;考虑到有可能两个数的最高位相加后有进位,所以两个数组都再额外多补充一位的0;
  4. 随后可以加一个for循环,将两个数组的所有对应位相加,对于每一位,如果和大于等于10,就进位,即下一位加1,当前为减10;
  5. 输出得到的结果。

程序:

#include "stdio.h"
#include "string.h" #pragma warning(disable:4996) int main()
{
char a_str[1000] = { 0 }, b_str[1000] = {0};
int a_num[1000], b_num[1000];
int a_len, b_len, max_len;
int n;
int i, j, k;
int temp; scanf("%d", &n);
if (n < 1 || n>20)
return -1; for (i = 1;i <= n;i++)
{
//步骤1
scanf("%s%s", a_str, b_str);
a_len = strlen(a_str);
b_len = strlen(b_str); //步骤2
temp = 0;
for (j = a_len - 1;j >= 0;j--)
{
a_num[temp++] = a_str[j] - '0';
} temp = 0;
for (k = b_len - 1;k >= 0;k--)
{
b_num[temp++] = b_str[k] - '0';
} //步骤3
if (a_len > b_len)
{
for (j = b_len;j <= a_len;j++)
{
b_num[j] = 0;
}
a_num[a_len] = 0;
}
else if (a_len < b_len)
{
for (j = a_len;j <= b_len;j++)
{
a_num[j] = 0;
}
b_num[b_len] = 0;
}
else
{
a_num[a_len] = 0;
b_num[b_len] = 0;
} max_len = (a_len >= b_len) ? a_len : b_len; //步骤4
for (j = 0;j <= max_len;j++)
{
a_num[j] += b_num[j];
if (a_num[j] >= 10)
{
a_num[j] -= 10;
a_num[j + 1] += 1;
}
} //步骤5
printf("Case %d:\n", i);
printf("%s + %s = ", a_str, b_str);
if (a_num[max_len] == 0)
{
for (j = max_len - 1;j >= 0;j--)
printf("%d", a_num[j]);
}
else
{
for (j = max_len;j >= 0;j--)
printf("%d", a_num[j]);
} if (i != n)
printf("\n\n");
else
printf("\n");
} return 0;
}

备注:

由于我是在visual studio 2015环境中调试这段程序的,在使用“scanf”函数时会报错。因为在microsoft的Visual Studio进行编译时,会提示“scanf”函数是不安全的,建议使用“scanf_s”函数来代替“scanf”。“scanf_s”函数在使用时,如果需要输入字符串数组,还需要指定边界,即数组的长度。一般情况下使用“scanf_s”函数就能替代“scanf”函数了,但是这题中却不行,因为我们也无法确定输入数组的长度。所以得出的结论就是,继续使用“scanf”函数,在程序加入这段代码:

#pragma warning(disable:4996)

这样就可以照常在程序中使用“scanf”而不报错了。

运行结果:

杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评的更多相关文章

  1. 杭电acm刷题顺序

    最近兴趣来了,闲暇之余,回顾大学期间刷过的杭电acm那些入门级别的题,以此巩固基础知识! 以下参考刷题顺序,避免入坑 原文传送门:https://blog.csdn.net/liuqiyao_01/a ...

  2. 杭电acm刷题(3):1062,Text Reverse 标签: 杭电acm 2017-05-15 08:26 126人阅读 评论(0)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  3. 杭电ACM刷题(2):1005,Number Sequence 标签: 杭电acmC语言 2017-05-11 22:43 116人阅读

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  4. 杭电acm 1076题

    水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...

  5. 杭电acm 1037题

    本题应该是迄今为止最为简单的一道题,只有一组输入,输出也简单.... /****************************************** 杭电acm 1037题 已AC ***** ...

  6. 杭电acm 1038题

    本题比较简单,但是需要掌握几个小技巧,先上代码 /************************************* 杭电ACM 1038题,已AC ********************* ...

  7. 杭电acm 1049题

    一道水题..... 大意是一条1inch的虫子在一个n inch的盒子的底部,有足够的能够每一分钟往上爬u inch,但是需要休息一分钟,这期间会往下掉d inch,虫子爬到盒子口即认为结束.要求计算 ...

  8. 杭电acm 1033题

    Problem Description For products that are wrapped in small packings it is necessary that the sheet o ...

  9. 杭电acm 1015题

    马上要找工作了,锻炼下自己的写程序能力,不多说,上代码 /********************杭电acm 1015 已AC 在这个程序里,使用穷举法来实现,但是输出顺序需要安装字典的最大 来输出 ...

随机推荐

  1. 条款36:绝对不要重新定义,继承而来的non-virtual函数

    重新定义一个继承而来的non-virtual函数可能会使得导致当函数被调用的时候,被调用的函数不是取决于调用的函数究竟属于的对象,而是取决于调用函数的指针或者引用的类型. 所以一般的说主要有两种观点在 ...

  2. java对Hbase的基本操作

     1.新建一个普通java项目,把${hbase}/lib/目录下的jar包全部导入 2.导出jar文件如下 3.运行 注意:需要先把jar文件导入到hbase路径里去,然后运行相应的类 4.查看数据 ...

  3. CentOS 6.8安装Docker V1.0

    rpm -Uvh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum -y install do ...

  4. 未定义的标示符“RECT”,引入了windows.h头文件也没有用?

    我用的是win8的vs2012,RECT应该引入什么头文件?windows.h我第一个就引入了,去windows.h里面搜也搜不到RECT这个关键字,应该引入哪个头文件呢? 真是奇怪啊,是不是还需要什 ...

  5. js 科学计数转数字或字符串

  6. 横向排列两个多个div盒子的方法(CSS浮动清除float-clear/inline)/办法

    最近在做一个div css切割,昨晚发现了长期以来一直无记录下来的问题!关于兼容IE跟FF的float属性.趁现在还清醒赶紧记下笔记先:一.并排在一行的两个div样式有这种情况:ie或者ff下对于子d ...

  7. 手机访问PC网站自动跳转到手机网站代码(转)

    4G时代,手机网站已经非常普遍了,一般手机网站都有一个二级域名来访问,比如 m.16css.com 如果手机直接访问www.16css.com 就是PC网站,在手机上浏览电脑版网站体验非常不好. 如果 ...

  8. BZOJ1202:[HNOI2005]狡猾的商人

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  9. ECMAScript 2016(ES7) 知多少

    ECMAScript 2016(ES7) 知多少 1. 数组方法 Array.prototype.includes(value : any) : boolean 2. 幂运算符 x ** y 扩展阅读 ...

  10. PyCharm 2017.2.2+PyQt5+Python3.6.0

    PyCharm注册地址 http://idea.imsxm.com/ 安装的是miniconda激活虚拟环境执行pip install PyQt5pip install PyQt5-tools 从官网 ...