Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define DIGITMAX 101 /* 100 + dummy */
- void recurringDecimal(int x, int y) {
- int rem[DIGITMAX], quo[DIGITMAX], r = 0, i, remainder, loop = 0;
- rem[r] = 0; quo[r++] = 0; /* dummy */
- printf("%d.", x / y);
- while (x != 0) {
- remainder = x % y;
- if (!remainder) break;
- for (i = 1; i < r; i++)
- if (rem[i] == remainder) { loop = i; goto looped; }
- rem[r] = remainder; x %= y; x *= 10; quo[r++] = x / y;
- if (r > DIGITMAX) break;
- continue;
- looped: break;
- }
- if (loop) {
- for (i = 1; i < r; i++) {
- if (i == loop) printf("["); printf("%d", quo[i]); if (i == r - 1) printf("]");
- }
- } else if (r <= DIGITMAX)
- for (i = 1; i < r; i++) printf("%d", quo[i]);
- else {
- for (i = 1; i < DIGITMAX; i++) printf("%d", quo[i]);
- printf("...");
- }
- puts("");
- }
- int main(void) {
- int x, y, i;
- recurringDecimal(1, 1);
- recurringDecimal(1, 3);
- recurringDecimal(2, 3);
- recurringDecimal(1, 8);
- printf(" "); for (i = 1; i < DIGITMAX; i++) printf("%d", i % 10); puts("");
- recurringDecimal(1, 991);
- recurringDecimal(1, 997);
- recurringDecimal(10, 89);
- recurringDecimal(1234, 555);
- puts("分子 分母 を正数で入力してください。");
- scanf("%d%d", &x, &y);
- if (x < 0) x = -x;
- if (y < 0) y = -y;
- printf("%d / %d = ", x, y);
- recurringDecimal(x, y);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement