大数加法 A+B C++

原文链接 https://dyingdown.github.io/2019/09/15/String-A-B/
A+B 的问题也许是最简单的问题了。但是,如果数据过大就没法处理了除非你用Python 或者是 Java 等。C/C++ 能计算的数据的范围是 -9223372036854775808~9223372036854775807。

既然我们不能用 int 或者 long int 或者 long long 类型,我们可以用string类型。

string a, b;
cin >> a >> b;

有了字符串,我们可以用小学学过的 按位加竖式运算。 从最后一位加起,逢十进一。字符串向后插入一个字符比向前插入一个更容易,所以我把字符串反转一下,正好可可以让末尾对其。

reverse(a.begin(), a.end());
reverse(b.begin(), b.end());

我们需要知道两个字符串之间差几位并且用字符0把差的位数补齐。

int diff = abs((int)(a.length() - b.length()));
if (a.length() < b.length()) {
     for(int i = 0; i < diff; i ++){
         a += '0';
     }   
} else {
    for(int i = 0; i < diff; i ++){
        b += '0';
    } 
}

注意t .length() 返回 unsigned long long, 直接做差会使数据溢出,因为不能是负数

然后我们开始对每一位进行运算,每一位有三个元素: a[i], b[i], carryIn.

int carryIn = 0, digit;
string ans = "";
for(int i = 0; i < a.length(); i ++){
    digit = (a[i] - '0') + (b[i] - '0') + carryIn; // convert char to int
    carryIn = digit / 10;
    digit %= 10;
    ans += digit + '0'; // convert int to char
} 

如果运算到最后一位结束还有一个进位,那就再增加一位。

 if(carryIn){
     ans += carryIn + '0';
 }

别忘记再把字符串反转回来。

reverse(ans.begin(), ans.end());
cout << ans << endl;

这是HDU 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

AC Code

#include

using namespace std;

int main(){
	int t; 
	string a, b, ans, mid, first;
	cin >> t;
	for(int i = 1; i <= t; i ++){
		ans = "";
		cin >> a >> b;
        cout << "Case " << i << ":" << endl << a << " + " << b << " = ";
		reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        if(a.length() < b.length()){
            mid = a;
            first = b;
        }else{
            mid = b;
            first = a;
        }
        int diff = abs((int)(a.length() - b.length()));
        for(int j = 0; j < diff; j ++){
            mid += '0';
        }    
        int carryIN = 0, digit;
        for(int j = 0; j < mid.length(); j ++){
            digit = (first[j] - '0') + (mid[j] - '0') + carryIN;
            carryIN = digit / 10;
            digit %= 10;
            ans += digit + '0';
        } 
        if(carryIN){
            ans += carryIN + '0';
        }
        reverse(ans.begin(), ans.end());
		cout << ans << endl;
		if(i != t) cout << endl;
	}
	return 0;
}

你可能感兴趣的:(ACM)