杭电ACM1002 A + B Problem II,错误大多是int溢出

Hdu 1002 A + B Problem II,

题目

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

分析

杭电的题目特别是英文题目一定要看的很仔细,然后仔细看看 要求的输入输出格式。
这道题目其实就是hdn1000题的一个进阶版,首先它要求增加一个通过输入用例个数来控制输入次数的,所以实现这个很简单,输入、for循环就OK! 其次请注意我在题目中加粗的那一段英文,它的意思是:注意,整数非常大,这意味着不应该使用32位整数来处理它们,简而言之,如果A,B和sum都有可能十分的大,大到不能用Int来作为他的数据类型,double也可能不够。所以我采用了字符串的形式来存储这个程序里面的数字,因为字符串的长度几乎是无限的,所以采用字符串的形式就可以存储了。那它们之间相加就没有这么简单了,这个后面在说。
最后仔细看题目中的输出,case1 和case2之间换行,但是最后case2后面并没有换行,所以必须要控制\n的输出次数,我这里采用的就是当当前用例小于总用例时就打印换行

if(i<num-1) {
			printf("\n");
		}

现在的问题就是:如何实现字符串相加?

首先我们小学时做加法运算,是把两个数字上下放两行,一根横线下面写计算结果,个位相加大于10,十位进一。

所以依照这个思路,先创建一个标记(flag),记录当前是否需要进一位,,然后将AB两个字符串从下标最大的那个字符开始相加,得到的sum还需要再加上flag,sum的值存入一个结果字符串C[]中,当然字符相加是需要将字符的类型转化为int类型的,代码如下sum = (int)a[idxA]-48+(int)b[idxB]-48+flag;
还有一个需要注意的地方就是,当AB长度不一样的是时候,就一定会出现最后只剩一个字符相加的时候,所以当A还有B已经加完了的时候,只需将B的值加上flag后存入C[]中。

至此思路大概已经理清楚了,当然我也是提交答案错误了很多次才得到的这个正确代码,人总是在不断的错误中成长起来的,也唯有错误才能使得记忆深刻。下面是完整代码:

改正后果不其然通过了,不过说实话,杭电的Accepted属实难受,通常不都是报错字体颜色才会选用红色吗?代码正确就是绿色~~无所谓啦

#include <stdio.h>
#include <string.h>
int main() {
	int num;
	int i;
	int j;
	char d;
	char a[11111];
	char b[11111];
	char c[11111];
	scanf("%d", &num);
	for (i=0; i<num; i++) {
		scanf("%s%s", a, b);
		int idxA = strlen(a)-1;
		int idxB = strlen(b)-1;

		int idxC = 0;
		int flag = 0;
		int sum = 0;
		while (idxA >= 0 || idxB >= 0) {
			if (idxA < 0) {	// a没有b还有
				sum = (int)b[idxB]-48+flag;
			} else if (idxB < 0) {	// b没有a还有
				sum = (int)a[idxA]-48+flag;
			} else {	// a和b都还有
				sum = (int)a[idxA]-48+(int)b[idxB]-48+flag;
			}
			flag = sum / 10;
			c[idxC] = (char)(sum%10+48);
			idxA--;
			idxB--;
			idxC++;
		}
		c[idxC] = '\0';
		int idxCL = strlen(c);
		for(j=0; j<idxCL/2; j++) {
			d=c[j];
			c[j]=c[idxCL-1-j];
			c[idxCL-1-j]=d;
		}
	

		printf("Case %d:\n%s + %s = %s\n", i+1, a, b, c);
		if(i<num-1) {
			printf("\n");
		}
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值