#include<stdio.h> #include<string.h> struct bign { int d[1010], len; bign() { memset(d, 0, sizeof(d)); len = 0; } }; bign change(char str[]) { bign a; a.len = strlen(str); for (int i = 0; i < a.len; i++) { a.d[i] = str[a.len - i - 1] - ‘0‘; // 不要忘记字符变成数字啊啊啊啊啊啊 } return a; } bign divide(bign a, int b, int &r) { bign c; c.len = a.len; for (int i = a.len - 1; i >= 0; --i) { r = a.d[i] + r * 10; //不要忘记将余数扩大十倍 if (r < b) c.d[i] = 0; else { c.d[i] = r / b; r = r % b; } } while (c.len > 1 && c.d[c.len - 1] == 0) { //注意c.len > 1 而不是 >= 1(PAT有被除数为’0‘的测试点但是牛客网没有) c.len--; } return c; } int main() { char str[1010]; int b, r = 0; scanf("%s %d", str, &b); bign a = change(str); bign c = divide(a, b, r); for (int i = c.len - 1; i >= 0; i--) { printf("%d", c.d[i]); } printf(" %d\n", r); }
原文地址:https://www.cnblogs.com/Yaxadu/p/9190737.html
时间: 2024-10-09 23:07:11