【描述】
求两个大的正整数相减的差。
【输入】
共2行,第1行是被减数,第2行是减数。每个大整数不超过200位,不会有多余的前导零。
【输出】
一行,即所求的差。
【样例输入】
9999999999999999999999999999999999999
9999999999999
【样例输出】
9999999999999999999999990000000000000
【参考程序】
C语言版本
- ttp://noi.openjudge.cn/ch0106/11/
- */
- # include <cstdio>
- # include <cstring>
- int main()
- {
- char s1[205], s2[205];
- scanf("%s", &s1);
- scanf("%s", &s2);
- int len1 = strlen(s1);
- int len2 = strlen(s2);
- int n1[205] = { 0 }, n2[205] = { 0 }, ans[205] = { 0 };
- for (int i = 0; i < len1; i++)
- {
- n1[i] = s1[len1 - i - 1] - '0';
- }
- for (int i = 0; i < len2; i++)
- {
- n2[i] = s2[len2 - i - 1] - '0';
- }
- int carry = 0;//进位
- int i = 0;
- while (i < len1)
- {
- int a = n1[i] - n2[i] - carry;
- if (a >= 0)
- {
- ans[i] = a;
- carry = 0;
- }
- else
- {
- ans[i] = a + 10;
- carry = 1;
- }
- i++;
- }
- while (ans[i] == 0 && i > 0)
- {
- i--;
- }
- for (int j = i; j >= 0; j--)
- {
- printf("%d", ans[j]);
- }
- return 0;
- }
C++版本
- # include <iostream>
- # include <cstring>
- using namespace std;
- int main()
- {
- string s1, s2;
- cin >> s1 >> s2;
- int len1 = s1.size();
- int len2 = s2.size();
- int n1[205] = { 0 }, n2[205] = { 0 }, ans[205] = { 0 };
- for (int i = 0; i < len1; i++)
- {
- n1[i] = s1[len1 - i - 1] - '0';
- }
- for (int i = 0; i < len2; i++)
- {
- n2[i] = s2[len2 - i - 1] - '0';
- }
- int carry = 0;//进位
- int i = 0;
- while (i < len1)
- {
- int a = n1[i] - n2[i] - carry;
- if (a >= 0)
- {
- ans[i] = a;
- carry = 0;
- }
- else
- {
- ans[i] = a + 10;
- carry = 1;
- }
- i++;
- }
- while (ans[i] == 0 && i > 0)
- {
- i--;
- }
- for (int j = i; j >= 0; j--)
- {
- cout << ans[j];
- }
- return 0;
- }