先输入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; }
时间: 2024-11-01 01:09:36