Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator and print the decimal representation. If the decimal representation has a repeating sequence of digits, indicate the sequence by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as 0.(3), and 41/333 = 0.123123123...is denoted as 0.(123). Use xxx.0 to denote an integer. Typical conversions are:
1/3 = 0.(3) 22/5 = 4.4 1/7 = 0.(142857) 2/2 = 1.0 3/8 = 0.375 45/56 = 0.803(571428)
A single line with two space separated integers, N and D, 1 <= N,D <= 100000.
45 56
The decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.
0.803(571428) /* ID: guangxue1 PROG: fracdec LANG: C++ */ #include <fstream> using namespace std; int memory[100000]; int result[100000]; int core(int N, int D,int i,int &A,int &B) { if(N==0) return i; result[i]=N*10/D; if(memory[N]!=0) {A=memory[N]-1,B=i; return -1;} memory[N]=i+1; core(N*10%D, D, ++i,A,B); } int main() { ifstream fin("fracdec.in"); ofstream fout("fracdec.out"); int length=0; int N,D; int A,B; fin>>N>>D; fout<<N/D; int NN=N; do ++length; while(NN=NN/D); fout<<'.'; ++length; if(N%D==0) { fout<<0<<endl; return 0;} else { int sign; sign=core(N%D,D,0,A,B); if(sign==-1) { for(int i=0; i<A; ++i) {fout<<result[i];++length; if(length==76) {fout<<endl; length=0;}} fout<<'('; ++length; if(length==76) {fout<<endl; length=0;} for(int i=A; i<B; ++i) {fout<<result[i]; ++length; if(length==76) {fout<<endl; length=0;}} fout<<')'<<endl; } else { for(int i=0; i<sign; ++i) { fout<<result[i]; ++length; if(length==76) {fout<<endl; length=0;}} fout<<endl; } } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define MAXDIGIT 100100 char dec[MAXDIGIT]; int lastrem[MAXDIGIT]; char buf[MAXDIGIT]; void main(void) { FILE *fin, *fout; int n, d, k, i, rem, len; fin = fopen("fracdec.in", "r"); fout = fopen("fracdec.out", "w"); assert(fin != NULL && fout != NULL); fscanf(fin, "%d %d", &n, &d); sprintf(buf, "%d.", n/d); /* long division keeping track of if we've seem a remainder before */ for(i=0; i<MAXDIGIT; i++) lastrem[i] = -1; rem = n % d; for(i=0;; i++) { if(rem == 0) { if(i == 0) sprintf(buf+strlen(buf), "0"); else sprintf(buf+strlen(buf), "%s", dec); break; } if(lastrem[rem] != -1) { k = lastrem[rem]; sprintf(buf+strlen(buf), "%.*s(%s)", k, dec, dec+k); break; } lastrem[rem] = i; n = rem * 10; dec[i] = n/d + '0'; rem = n%d; } /* print buf 76 chars per line */ len = strlen(buf); for(i=0; i<len; i+=76) { fprintf(fout, "%.76s/n", buf+i); } exit(0); }
#include <iostream.h> #include <fstream.h> #include <math.h> ofstream out("fracdec.out"); int colcount=0; int numBeforeRepeat(int n, int d) { int c2=0, c5=0; if (n == 0) return 1; while (d%2==0) { d/=2; c2++; } while (d%5==0) { d/=5; c5++; } while (n%2==0) { n/=2; c2--; } /* can go negative */ while (n%5==0) { n/=5; c5--; } /* can go negative */ if (c2>c5) if (c2>0) return c2; else return 0; else if (c5>0) return c5; else return 0; } void print (char c) { if (colcount==76) { out<<endl; colcount=0; } out<<c; colcount++; } void print (int n) { if (n>=10) print (n/10); print ((char)('0'+(n%10))); } int main() { int n, d; ifstream in("fracdec.in"); in>>n>>d; in.close(); print (n/d); print ('.'); n=n%d; int m=numBeforeRepeat(n,d); for(int i=0;i<m;i++) { n*=10; print (n/d); n%=d; } int r=n; if(r!=0) { print ('('); do { n*=10; print (n/d); n%=d; } while (n!=r); print (')'); } out<<endl; out.close(); exit (0); }