当前位置:网站首页>A - A + B Problem II
A - A + B Problem II
2022-08-09 00:12:00 【霍瑟夫】
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
#include<iostream>
#include <string>
using namespace std;
int main() {
int T; //行数
cin >> T;
int A[1001], B[1001],C[1001]; //字符串转 int 类型数组
string a, b; //字符串
int al, bl; //字符串长度
for (int i = 1; i <= T; i++) {
//进行T次
for (int j = 1; j <= 1001; j++) {
//初始化数组全为 0
A[j - 1] = 0;
B[j - 1] = 0;
C[j - 1] = 0;
}
cin >> a >> b; //输入字符串 a,b
al = a.length(); bl = b.length();
int cl = al > bl ? al : bl; //a,b 中更长的字符串长度
for (int j = 0; j < al; j++) {
//为A数组赋值(逆序)
A[j] = a[al-j-1] - '0';
}
for (int j = 0; j < bl; j++) {
//为B数组赋值(逆序)
B[j] = b[bl-j-1] - '0';
}
for (int j = 0; j < cl; j++) {
//求和(逆序)
C[j] = C[j] + A[j] + B[j];
if (C[j] >= 10) {
C[j] = C[j] - 10;
C[j + 1] = 1;
}
}
cout << "Case " << i << ":" << endl;
cout << a << " + " << b << " = ";
if (C[cl] != 0) {
//判断最高位是否进位
cl = cl + 1;
}
for (int j = 0; j < cl; j++) {
//逆序输出
cout << C[cl-j-1];
}
cout << endl ;
if (i < T) {
cout << endl;
}
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
牛客小白月赛 37 补题
MySQL中varchar 的最大长度
2020-10-17
Ubuntu下Docker安装Mysql (快速简便)
整流七 - 三相PWM整流器—公式推导篇
整流八--电网不平衡状态下三相PWM整流器的控制策略
穿越派如何续购相关产品功能
如何购买穿越派V3.14版本产品?
牛客多校8 补题
Shader实现帧动画-FrameAnimation
蓝桥杯历届试题-合根植物(并查集)
微信小程序 【控制台报错-汇总】
无代码平台邮箱入门教程
C#编写飞行棋游戏
移动web开发-插件&事件篇
C--《C和指针》第8章读书笔记之效率问题
渐变纹理(光照渐变)
对于js中apply和call的区别和用法
对纹理进行uv坐标偏移
Task19_14_最长公共前缀