#143-[高精度]高精度除高精度
阿新 • • 發佈:2018-12-17
Description
輸入兩個整數x,y,輸出它們的商和餘數。
Input
輸入兩個整數x,y(0 <= x,y <= 10^100)
Output
輸出共計兩行,第一行為它們的商,第二行為他們的餘數
Sample Input
123
12
Sample Output
10
3
模板
#include <iostream> #include <cstring> #include <string> #define SIZE 1010 using namespace std; string s; int a[SIZE], b[SIZE], temp[SIZE], lena, lenb, i; bool comp(void) // 比較大小 { int i; if (lena < lenb + ::i) { return false; } if (lena > lenb + ::i) { return true; } for (i = lena; i; --i) { if (a[i] < temp[i]) { return false; } if (a[i] > temp[i]) { return true; } } return true; } void sub(void) // 減法 { int i; for (i = 1; i <= lena; ++i) { a[i] -= temp[i]; if (a[i] < 0) { --a[i+1]; a[i] += 10; } } while ((!a[lena]) && (lena)) { --lena; } return; } int main(void) { int len, c, j; bool flag = false; cin >> s; lena = s.size(); for (i = 1; i <= lena; ++i) { a[i] = s[lena-i] - '0'; } cin >> s; lenb = s.size(); for (i = 1; i <= lenb; ++i) { b[i] = s[lenb-i] - '0'; } len = max(lena - lenb, 0); for (i = len; ~i; --i) // 模板,不用說了 { memset(temp, 0, sizeof (temp)); for (j = 1; j <= lenb; ++j) { temp[i+j] = b[j]; } c = 0; while (comp()) { ++c; sub(); } if (c) { flag = true; } if (flag) { printf("%d", c); // 輸出商 } } if (!flag) { printf("0"); } printf("\n"); for (i = lena; i; --i) { printf("%d", a[i]); } if (!lena) { printf("0"); } return 0; }