標籤:
先輸入n ,代表共有幾組數字,再輸入n組資料
#include<iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<malloc.h>using namespace std;char resu[10000];void Reverse(char a[10000]){ int leng = strlen(a); char temp; for (int i = 0; i < leng / 2; i++) { temp = a[leng - i - 1]; a[leng - i - 1] = a[i]; a[i] = temp; } return ;}char *toAdd(char a[10000], char b[10000]){ Reverse(a); Reverse(b); char min[10000], max[10000]; if (strlen(a) >= strlen(b)) { strcpy_s(min, b); strcpy_s(max, a); } else { strcpy_s(min, a); strcpy_s(max, b); } int jinwei = 0; for (int i = 0; i < (int)strlen(min); i++) { if (min[i] + max[i] - ‘0‘ - ‘0‘ + jinwei>=10) { resu[i] = min[i] + max[i] - ‘0‘ + jinwei - 10; jinwei = 1; } else { resu[i] = min[i] + max[i] - ‘0‘ + jinwei; jinwei = 0; } } int i; for (i = strlen(min); i < (int)strlen(max); i++) { if (max[i]- ‘0‘ + jinwei>=10) { resu[i] =max[i]+ jinwei - 10; jinwei = 1; } else { resu[i] =max[i]+ jinwei; jinwei = 0; } } if (jinwei) { resu[i] = jinwei + ‘0‘; resu[++i] = ‘\0‘; } else resu[i] = ‘\0‘; Reverse(resu); return resu;}int main( ){ int n; char a[20][10000], b[20][10000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } for (int i = 0; i < n; i++) { cout << "Case " << i+1 << ":" << endl; cout << a[i] << " + " << b[i] << " = "; toAdd(a[i], b[i]); int k = 0; while (resu[k] == ‘0‘) k++; if (k == strlen(resu)) { cout << "0"; cout << endl; } else { for (int o = k; o < strlen(resu);o++) cout << resu[o]; cout << endl; } if (i <( n - 1)) cout << endl; } return 0;}
杭電OJ1002大資料相加