202 - Repeating Decimals
Posted guopinghai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了202 - Repeating Decimals相关的知识,希望对你有一定的参考价值。
#include <stdio.h> #include <string.h> using namespace std; int isExist(int* list, int size, int val); int main() int m, n, divided[5000], count, idx, flag; char decimal[5000]; while(scanf("%d%d", &m, &n) != EOF) count = 0, idx = -1; memset((void *)divided, ‘\0‘, 500); memset(decimal, ‘\0‘, 500); printf("%d/%d = %d.", m, n, m / n); m = m % n; while(true) m = m * 10; idx = isExist(divided, count, m); if(idx != -1) break; divided[count] = m; decimal[count] = (m / n) + ‘0‘; m = m % n; count++; for(int i = 0; i < idx; ++i) if(i >= 50) break; printf("%c", decimal[i]); printf("("); for(int i = idx; i < count; ++i) if(i >= 50) printf("..."); break; printf("%c", decimal[i]); printf(")\n"); printf(" %d = number of digits in repeating cycle\n\n", count - idx); int isExist(int* list, int size, int val) for(int i = 0; i < size; ++i) if(list[i] == val) return i; return -1; // Sample Input // 76 25 // 5 43 // 1 397 // Sample Output // 76/25 = 3.04(0) // 1 = number of digits in repeating cycle // 5/43 = 0.(116279069767441860465) // 21 = number of digits in repeating cycle // 1/397 = 0.(00251889168765743073047858942065491183879093198992...) // 99 = number of digits in repeating cycle //https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=830&page=show_problem&problem=138 //还是要注意整除与取余在数字运算中的重要作用 //代码能力!!!
以上是关于202 - Repeating Decimals的主要内容,如果未能解决你的问题,请参考以下文章
[算法竞赛入门经典]Repeating Decimals, ACM/ICPC World Finals 1990,UVa202