#include <iostream> #include <cstdlib> #include <cmath> #include <deque> #include <algorithm> #include <numeric> #include <iterator> #include <sstream> #include <iomanip> #include <vector> #include <string.h> #include <string> using namespace std; void Add(char *str1, char *str2, char *str3)//大数的加法 { // str3 = str1 + str2; int i, j, i1, i2, tmp, carry; int len1 = (int)strlen(str1), len2 = (int)strlen(str2); char ch; i1 = len1 - 1; i2 = len2 - 1; j = carry = 0; for (; i1 >= 0 && i2 >= 0; ++j, --i1, --i2) { tmp = str1[i1] - '0' + str2[i2] - '0' + carry; carry = tmp / 10; str3[j] = tmp % 10 + '0'; } while (i1 >= 0) { tmp = str1[i1--] - '0' + carry; carry = tmp / 10; str3[j++] = tmp % 10 + '0'; } while (i2 >= 0) { tmp = str2[i2--] - '0' + carry; carry = tmp / 10; str3[j++] = tmp % 10 + '0'; } if (carry) { str3[j++] = carry + '0'; } str3[j] = '\0'; for (i = 0, --j; i < j; ++i, --j) { ch = str3[i]; str3[i] = str3[j]; str3[j] = ch; } return ; } const int MAXSIZE = 1001; int main() { char str1[MAXSIZE], str2[MAXSIZE], str3[MAXSIZE]; scanf("%s %s", str1, str2); if (strcmp(str1, "0")) { memset(str3, '0', sizeof(str3)); Add(str1, str2, str3); printf("%s\n", str3); } else { if (strcmp(str2, "0")) { printf("%s\n-%s\n0\n0\n", str2, str2); } else { printf("0\n0\n0\n0\n"); } } return 0; }
解题思路:
注意事项:
参考代码:
0.0分
0 人评分
不容易系列2 (C语言代码)浏览:650 |
假币问题 (C++代码)(向上取整的一种处理方式)浏览:1663 |
陶陶摘苹果 (C语言代码)浏览:1606 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:732 |
C语言训练-计算1977!* (C++代码)浏览:856 |
求圆的面积 (C语言代码)浏览:1270 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:543 |
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)浏览:2080 |
【蟠桃记】 (C语言代码)浏览:664 |
简单的a+b (C语言代码)浏览:631 |