算法:Problem - 1002: A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 560536
Accepted Submission(s): 106899

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.
一个很简单的问题,给出两个整数A和B,计算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.
第一行的输入为一个整数T(1≤T≤20),来表示问题的个数。之后的每行含有两个正整数A和B,要注意的是这两个整数可能非常大,这意味着你不应该用32位整数来处理它们。你可以假设每个整数的长度不超过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.
你应该为每个问题输出两行。第一行为“Case #”,#位置为问题的序号。第二行为一个等式“A + B = Sum”,Sum位置为A+B的结果。注意这个等式中有一些空格。每个问题的输出间要有一个空行。

Sample Input【输入示例】

2
1 2
112233445566778899 998877665544332211

Sample Output【输出示例】

Case 1:
1 + 2 = 3
.
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

Author【作者】

Ignatius.L

Recommend【相关推荐】

We have carefully selected several similar problems for you: 1004 1003 1008 1005 1089

My Code in C++【我的C++代码】

#include<iostream>
#include<string>
using namespace std;
int main() {
    unsigned short cas; //用来记录问题数T
    cin >> cas; 
    string* AL = new string[cas], * BL = new string[cas]; //用来记录每个问题的加数A,B
    int i;
    for (i = 0; i < cas; i++) cin >> AL[i] >> BL[i]; //记录输入的A,B
    string c; //用来记录当前问题的和Sum
    char carry, ans; //carry用来记录和的进位,ans用来表示和的当前位
    for (i = 0; i < cas; i++) { //依次运算每个问题并输出
        c.clear(); //每处理一个问题时,先将c置空
        string* A, * B;
        if (AL[i].length() > BL[i].length()) {
            A = &AL[i];
            B = &BL[i];
        }
        else {
            A = &BL[i];
            B = &AL[i];
        }//用A指向和数中较长的,用B指向和数中较短的
        carry = ans = 0; //运算前先将进位和当前位置零
        int j, len=(*A).length()-(*B).length(); //len表示两个加数的长度差
        for (j =(*A).length()-1; j >= len; j--) { //当A和B的当前位都有值时,从后向前加
            ans = ((*A)[j] - '0' + (*B)[j-len] - '0' + carry);
            carry = ans > 9 ? 1 : 0;
            c = (char)(carry > 0 ? ans - 10 + '0' : ans + '0') + c;
        }
        for (j= len - 1; j >= 0; j--) { //只有一个加数的当前位有值,继续向前加
            ans = ((*A)[j] - '0' + carry);
            carry = ans > 9 ? 1 : 0;
            c = (char)(carry > 0 ? ans - 10 + '0' : ans + '0') + c;
        }
        if (carry) c = '1' + c; //加数加完还有一个进位时,再向前加一个进位
        cout << "Case " << i + 1 << ":" << endl << AL[i] << " + " << BL[i] << " = " << c << endl; //输出
        if (i < cas - 1) cout << endl; //问题不为最后一个时,多输出一个换行符
    }
    return 0;
}

Realtime Status【测试结果】

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
363513962021-07-26 00:22:17Accepted100231MS1868K1223 BC++Best1thCODER

My Code in C【我的C代码】

#include"stdio.h"
int main() {
	unsigned char AL[20][1001], BL[20][1001], CL[1002], cas, ans, carry;
	unsigned short i, j, alen[20], blen[20];
	short pa, pb, pc;
	unsigned char* a, * b;
	scanf("%d", &cas);
	for (i = 0; i < cas; i++) {
		scanf("%s %s", AL[i], BL[i]);
		for (j = 0; AL[i][j] != 0; j++) AL[i][j] -= '0';
		alen[i] = j;
		for (j = 0; BL[i][j] != 0; j++) BL[i][j] -= '0';
		blen[i] = j;
	}
	for (i = 0; i < cas; i++) {
		if (alen[i] > blen[i]) {
			a = AL[i];
			b = BL[i];
		}
		else {
			a = BL[i];
			b = AL[i];
			j = alen[i];
			alen[i] = blen[i];
			blen[i] = j;
		}
		pa = alen[i] - 1;
		pb = blen[i] - 1;
		pc = alen[i];
		CL[pc--] = '\0';
		ans = carry = 0;
		while (pb >= 0) {
			ans = a[pa] + b[pb] + carry;
			a[pa] += '0';
			b[pb] += '0';
			carry = ans > 9 ? 1 : 0;
			ans = carry == 0 ? ans : ans - 10;
			CL[pc] = ans+'0';
			pa--; pb--; pc--;
		}
		while (pa >= 0) {
			ans = a[pa] + carry;
			a[pa] += '0';
			carry = ans > 9 ? 1 : 0;
			ans = carry == 0 ? ans : ans - 10;
			CL[pc] = ans + '0';
			pa--; pc--;
		}
		printf("Case %d:\n", i + 1);
		printf("%s + %s = %s%s\n", AL[i], BL[i], carry ? "1" : "", CL);
		if (i < cas - 1)printf("\n");
	}
	return 0;
}

Realtime Status【测试结果】

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
363523682021-07-26 10:00:33Accepted100215MS1744K1190 BCBest1thCODER
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lcy_Knight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值