Repeating decimals
The decimal expansion of the fraction 1/33 is, where the was used to indicate that the cycle repeats indefinitely with No intervening digits. In fact, the decimal expansion of every rational number (fraction) had 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 t o The right of the decimal is repeats indefinitely with no intervening digits. Thus For example, the repeating cycle of the fraction 1/250 are 0, which begins at position 4 (as opposed to 0 which begins At positions 1 or 2, and as opposed to-which begins at positions 1 or 4).
Input
Each line of the input file consists of a integer numerator, which is nonnegative, followed by an integer denominator, wh Ich 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 O f the decimal or the 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 places, place a left parenthesis where the cycle begins- It'll begin within the first 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)
+ = number of digits in repeating cycle
1/397 = 0. (00251889168765743073047858942065491183879093198992 ...)
digits in repeating cycle
Test instructions
When a is divided by B. There is a conclusion: the result of a positive integer A/integer b, the fractional part is always a finite fractional or infinite loop decimal. The subject: If there is a repeating decimal, mark it out with () and calculate the length of the repeating decimal; if not, the end is indicated by (0).
Problem Solving Ideas:
1. The application of pigeon nest principle (or drawer principle) namely: There are ten apples on the table, in order to put the ten apples in nine drawers, each drawer represents a set, each apple can represent an element, if there are n+1 elements put in n sets, there must be at least one set of two elements.
Application in the subject, A/b, then according to the modulo operation, you can know that the remainder must be in the 0~b-1 range, then when the operation of b+1 times, must repeat a remainder (according to the Pigeon Nest principle). When you find the remainder that begins to repeat, you can find a follow-up link.
2. Use Q[max] to record the quotient of each operation, R[max] to record the remainder of each operation, while setting the tag array Vist[max], to mark whether the remainder has occurred
For example 5/7 Q[0]~q[6] array 0 7 1 4 2 8 5; R[0]~R[6] in turn 5 5 1 3 2 6 4
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intMAX = the;7 intY[max];//Array Storage remainder8 intS[max];//Array storage provider9 intBj[max];//Mark whether the number I appearsTen One A intMain () - { - intA, B; the - - while(Cin >> a >>b) - { +memset (Y,0,sizeof(y)); -memset (s),0,sizeof(s)); +memset (BJ,0,sizeof(BJ)); A at intA_temp = A;//Save the value of a - intCNT =0;// -s[cnt++] = A/b;//Quotient of Division of AB (integer part) -A = a%b;//find the remainder of division AB - while(A&&!bj[a])//A does not appear for 0 && A in the tag array - { inBj[a] =1;//Mark remainder a -S[CNT] =Ten* A/b;//Recycling Store Operators toY[CNT] = A;//circulating remainder +A =Ten* a%b; -cnt++;//in the last cycle, CNT has been added once. the } *cout << a_temp <<"/"<< b <<"="<< s[0] <<"."; $ Panax Notoginseng if(A = =0) - { the for(inti =1; i<cnt; i++) +cout <<S[i]; Acout <<"(0)"<<Endl; thecout <<"1=number of digits in repeating cycle"<<Endl; + - } $ Else $ { - intPos; - for(inti =1; I <= -&& i<cnt; i++) the { - if(Y[i] = = a)//find the position of the value A in the remainder arrayWuyi { thecout <<"("; -pos =i; Wu } -cout << S[i];//the number of the loop output loop part About } $ if(cnt> -) -cout <<"....)"<<Endl; - Else -cout <<")"<<Endl; Acout << Cnt-pos <<"=number of digits in repeating cycle"<<Endl; + the } - $ } the GetChar (); the GetChar (); the return 0; the}
UVA202 Repeating decimal repeating decimals