Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1002
The idea of solving problems: using arrays to add large numbers. The built-in data types do not meet the requirements of the integer digits.
Examples:
<span style= "White-space:pre" ></span>1 999
<span style= "White-space:pre" ></span>999 1
#include <iostream> #include <cstring>using namespace std;int num1[1010],num2[1010];int t[1010];int result [1010];char str1[1010],str2[1010];void Add (int len1,int len2) {int i,j;//character converted to a number, then invert for (i = 0;i < len1;i++) T[i] = str1[ I]-48;for (i = len1-1,j = 0;i >= 0;i--, j + +) Num1[j] = t[i];for (i = 0;i < len2;i++) T[i] = Str2[i]-48;for (i = len2) -1,j = 0;i >= 0;i--, j + +) Num2[j] = T[i];int k = 0,temp,cnt = 0;i = 0;while (i < len1 | | I < LEN2) {temp = Num1[i] + num2[i] + cnt;//cnt mean carry if (temp >=) {cnt = 1;result[i] = temp% 10;} Else{result[k] = temp;cnt = 0;} i++;k++;} RESULT[K] = cnt;//If it is 99+99, the final carry 1 is recorded for (i = 0;i < len1;i++) cout << str1[i];cout << "+"; for (i = 0;i &L T len2;i++) cout << str2[i];cout << "="; if (result[k]! = 0) cout << result[k];int ii;for (ii = K-1;II >= 0;ii--) {cout << result[ii];} cout << Endl;} int main (int argc, char *argv[]) {int s,cnt = 1;cin >> s; int a = s; while (s--) {memset (num1,0,sizeof (NUM1)); Memset (Num2,0,sizeof (num2));//Initialize NUM1 and num2 arrays each time, no WA memset (result,0,sizeof ( result), CIN >> str1 >> str2;cout << "case" << cnt << ":" << Endl;add (strlen (str1), St Rlen (STR2)); if (CNT < a)//The last sample does not have a blank line cout << endl;cnt++;} return 0;}
Hdu_1013_a + B Problem Ii_ (analog)