Uva202 - Repeating Decimals

#include<stdio.h>
#include<string.h>
int fraction[4000];
int mod[4000];
int main(){
	int a, b;
	int q,p,len;
	int d = 0;
	while (scanf("%d%d", &a, &b) != EOF){
		mod[0] = a;
		fraction[0] = a / b;
		for (q = 1;; q++){
			mod[q] = (mod[q - 1] - b*fraction[q - 1]) * 10;
			fraction[q] = mod[q] / b;
			for ( p = 1;p<q; p++){
				if (mod[p] == mod[q]){
					len = q - p;
					d = 1; break;
				}
			}
			if (d) break;
		}
		printf("%d/%d = %d.", a, b, fraction[0]);
		for (int i = 1; i < p; i++){
			printf("%d", fraction[i]);
		}
		printf("(");
		for (int i = p; i < q; i++){
			if (i >50){
				printf("...");
				break;
			}
			printf("%d", fraction[i]);
		}
		printf(")\n   %d = number of digits in repeating cycle\n\n", len);
		d = 0;
	}
	return 0;
}

你可能感兴趣的:(Uva202 - Repeating Decimals)