2016-07-05 Problem Solving►UVa UVa 275 - Expanding Fractions Contents 1. Problem2. Solution3. Code Problem題目網址中文網址 Solution利用長除法的方式,邊乘 10 邊除,當餘數已經重複出現時,即開始循環了。記下餘數出現的順序,就可知道從哪開始循環了。 p.s. 循環節的長度不會大於分母。 CodeUVa 275123456789101112131415161718192021222324252627282930313233343536373839404142#include<cstdio>int main(){ int n, m; int order[1000]; while (scanf("%d%d", &n, &m) && m) { int count = 1; int remain[1000] = {};//餘數出現的順序 while (n) { remain[n] = count; order[count++] = (n * 10) / m;//商 n = (n * 10) % m;//餘數 if (remain[n]) break; } int i; putchar('.'); for (i = 1; i < count; i++) { if (!(i % 50)) putchar('\n'); printf("%d", order[i]); } putchar('\n'); if (!n) puts("This expansion terminates.\n"); else printf("The last %d digits repeat forever.\n\n", count - remain[n]); } return 0;} Newer Machine Learning Foundations - L2 Notes(上) Older UVa 130 - Roman Roulette