The main idea is to take repeating decimal's circulation section
More clear thinking
Completely simulates the division of the human brain, while grouping marks dividend and divisor, judging the cyclic section
The middle of the need to judge is to divide and not to do two things
Finally, pay attention to the format, each two output to have a blank line, and the second line has 3 spaces before
A bit of code
1#include <cstdio>2#include <cstring>3 using namespacestd;4 5 #defineREP (n) for (int o=0;o<n;o++)6 7 structnode{8 intb;9 };Ten One BOOLDo (); A intVisited (intAintB,node n[],intlen); - intMain () { - //freopen ("In.txt", "R", stdin); the while(Do ()); - return 0; - } - + BOOLDo () { - intb; +Node mark[10000]; A intCnt=1; at int decimal[10000]; - - if(SCANF ("%d%d", &a,&b) ==eof)return false; -REP (10000) mark[o]={0, b}; -REP (10000)decimal[o]=-1; - in intx= (a%b) *Ten; - while(1){ to //printf ("%d/%d =%d ...%d mark[%d]={%d,%d}\n", x,b,x/b,x%b,cnt,x,b); + decimal[cnt]=x/b; - if(Visited (x,b,mark,cnt)) Break; themark[cnt]={x,b}; *x= (x%b) *Ten; $cnt++;Panax Notoginseng } -cnt--; the //REP (cnt+1) printf ("mark[%d]={%d,%d}\n", o,mark[o].a,mark[o].b); + intBegin=visited ((mark[cnt].a%b) *Ten, mark[cnt].b,mark,cnt); A the //printf ("\n\n%d\n\n", begin); +printf"%d/%d =%d.", a,b,a/b); - for(intI=1; i<=cnt;i++){ $ if(begin==i) printf ("("); $printf"%d",decimal[i]); - if(i== -){ -printf"..."); the Break; - }Wuyi } the if(begin==0) printf ("(0"); -printf") \ n%d = number of digits in repeating cycle\n\n", begin==0?1: cnt-begin+1); Wu return true; - } About $ intVisited (intAintB,node n[],intLen) { - for(into=1; o<=len;o++){ - if(n[o].a==a&&n[o].b==b) { - returno; A } + } the return 0; -}
Uva 202.Repeating Decimals