The decimal expansion of the fraction 1/33 is , where the is used to indicate that the cycle 03 repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number (fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have no such repeating cycles.
Examples of decimal expansions of rational numbers and their repeating cycles are shown below. Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.
Write a program that reads numerators and denominators of fractions and determines their repeating cycles.
For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal length string of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thus for example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0 which begins at positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).
Input
Each line of the input file consists of an integer numerator, which is nonnegative, followed by an integer denominator, which is positive. None of the input integers exceeds 3000. End-of-file indicates the end of input.
Output
For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycle to the right of the decimal or 50 decimal places (whichever comes first), and the length of the entire repeating cycle.
In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle begins - it will begin within the first 50 places - and place ...)" after the 50th digit.
Print a blank line after every test case.
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
题目类型: 字符处理题
算法分析:本题考查的是高精度除法的使用,在函数Division中最为关键的是将数据记录在record数组中并使用cnt数组来判断循环节。注意每个测试用例输出的最后是一个空行,下面第一个代码是更好的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
/************************************************** filename :n.cpp author :maksyuki created time :2017/12/2 13:17:13 last modified :2017/12/2 20:38:57 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int v[maxn], p[maxn]; bool vis[maxn]; int main() { #ifdef LOCAL CFF; //CFO; #endif int va, vb; while(scanf(" %d %d", &va, &vb) != EOF) { int len = 0, ta = va, tb = vb; v[len++] = ta / tb; ta = ta % tb; memset(vis, false, sizeof(vis)); memset(p, 0, sizeof(p)); while(ta && !vis[ta]) { vis[ta] = true; p[len] = ta; ta *= 10; v[len++] = ta / tb; ta %= tb; } printf("%d/%d = %d.", va, vb, v[0]); int i, j, pos = -1; for(i = 1, j = 1; i < len && j <= 50; i++, j++) { if(p[i] == ta) { pos = i; break; } else printf("%d", v[i]); } printf("("); if(!ta) printf("0"); else { for(; i < len && j <= 50; i++, j++) printf("%d", v[i]); } if(j > 50) printf("..."); puts(")"); if(!ta) printf(" 1"); else printf(" %d", len - pos); printf(" = number of digits in repeating cycle\n", len - pos); puts(""); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include <iostream> #include <fstream> #include <cstring> using namespace std; const int maxn = 6000 + 166; int record[maxn]; bool cnt[maxn]; int mod[maxn]; void Division (int nume, int denom) { memset (record, 0, sizeof (record)); memset (cnt, false, sizeof (cnt)); memset (mod, 0, sizeof (mod)); record[0] = nume / denom; cout << nume << "/" << denom << " = " << record[0] << "."; nume %= denom; int weishu = 1; while (nume && !cnt[nume]) { cnt[nume] = true; mod[weishu] = nume; nume *= 10; record[weishu++] = nume / denom; nume %= denom; } int i; for (i = 1; i < weishu && i <= 50; i++) { if (nume && mod[i] == nume) cout << "("; cout << record[i]; } if (nume == 0) cout << "(0"; if (i > 50) cout << "..."; cout << ")" << endl; cout << " "; if (!nume) cout << "1"; else { for (i = 0; i < weishu; i++) if (mod[i] == nume) break; cout << weishu - i; } cout << " = number of digits in repeating cycle" << endl; } int main() { // ifstream cin ("aaa.txt"); int nume, denom; while (cin >> nume >> denom) { Division (nume, denom); cout << endl; } return 0; } |
- « 上一篇:uva136
- uva213:下一篇 »