Hướng dẫn giải của Convert to Fraction
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của RR
{$MODE OBJFPC} {$R+,Q+} uses math; const FINP=''; FOUT=''; type big=array[0..100] of longint; var f1,f2:text; s,ss:string; test:longint; one,u,a,b,x,y,tu,mau,g:big; procedure scan(var u:big); var i:longint; begin fillchar(u,sizeof(u),0); u[0]:=length(ss); for i:=1 to u[0] do begin u[i]:=ord(ss[u[0]-i+1])-ord('0'); end; end; operator * (a,b:big) c:big; var i,j:longint; nho:int64; begin fillchar(c,sizeof(c),0); c[0]:=a[0]+b[0]-1; for i:=1 to a[0] do for j:=1 to b[0] do inc(c[i+j-1],a[i]*b[j]); nho:=0; for i:=1 to c[0] do begin c[i]:=c[i]+nho; nho:=c[i] div 10; c[i]:=c[i] mod 10; end; while (nho>0) do begin inc(c[0]); c[c[0]]:=nho mod 10; nho:=nho div 10; end; end; operator + (a,b:big) c:big; var i,nho:longint; begin fillchar(c,sizeof(c),0); nho:=0; c[0]:=max(a[0],b[0]); for i:=1 to c[0] do begin c[i]:=a[i]+b[i]+nho; if (c[i]<10) then nho:=0 else begin nho:=1; dec(c[i],10); end; end; if (nho>0) then begin inc(c[0]); c[c[0]]:=nho; end; end; operator - (a,b:big) c:big; var i,nho:longint; begin fillchar(c,sizeof(c),0); nho:=0; c[0]:=a[0]; for i:=1 to c[0] do begin c[i]:=a[i]-b[i]-nho; if (c[i]>=0) then nho:=0 else begin nho:=1; inc(c[i],10); end; end; while (c[0]>1) and (c[c[0]]=0) do dec(c[0]); end; procedure chia2(a:big; var b:big); var i,nho:longint; begin fillchar(b,sizeof(b),0); b[0]:=a[0]; nho:=0; for i:=a[0] downto 1 do begin b[i]:=(a[i]+nho*10)>>1; nho:=a[i] and 1; end; while (b[0]>1) and (b[b[0]]=0) do dec(b[0]); end; operator = (a,b:big) c:boolean; var i:longint; begin if a[0]<>b[0] then exit(false); for i:=1 to a[0] do if a[i]<>b[i] then exit(false); exit(true); end; operator < (a,b:big) c:boolean; var i:longint; begin if (a[0]<b[0]) then exit(true) else if (a[0]>b[0]) then exit(false); for i:=a[0] downto 1 do begin if a[i]<b[i] then exit(true) else if a[i]>b[i] then exit(false); end; exit(false); end; operator <= (a,b:big) c:boolean; var i:longint; begin if (a[0]<b[0]) then exit(true) else if (a[0]>b[0]) then exit(false); for i:=a[0] downto 1 do begin if a[i]<b[i] then exit(true) else if a[i]>b[i] then exit(false); end; exit(true); end; procedure print(a:big); var i:longint; begin for i:=a[0] downto 1 do write(f2,a[i]); end; operator / (a,b:big) c:big; var l,r,mid:big; begin fillchar(l,sizeof(l),0); l[1]:=0; l[0]:=1; r:=a; while (l<=r) do begin chia2(l+r,mid); if (mid*b<=a) then begin l:=mid+one; c:=mid; end else r:=mid-one; end; end; function gcd(x,y:big):big; var res:big; begin fillchar(res,sizeof(res),0); res[0]:=1; res[1]:=1; while (x[1] and 1=0) and (y[1] and 1=0) do begin res:=res+res; chia2(x,x); chia2(y,y); end; while not (x=y) do begin if (x<y) then y:=y-x; if (y<x) then x:=x-y; if (x[1] and 1=0) then chia2(x,x); if (y[1] and 1=0) then chia2(y,y); end; exit(res*x); end; procedure solve; var i:longint; begin fillchar(x,sizeof(x),0); x[0]:=a[0]+1; x[x[0]]:=1; fillchar(y,sizeof(y),0); y[0]:=b[0]+a[0]; for i:=1 to y[0] do y[i]:=9; for i:=1 to a[0] do y[i]:=0; tu:=u*x*y+a*y+b*x; mau:=x*y; if (tu[1]=0) and (tu[0]=1) then begin writeln(f2,'0/1'); exit; end; g:=gcd(tu,mau); print(tu/g); write(f2,'/'); print(mau/g); writeln(f2); end; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); readln(f1,test); one[0]:=1; one[1]:=1; for test:=1 to test do begin readln(f1,s); while (s[length(s)]<>')') do delete(s,length(s),1); ss:=copy(s,1,pos('.',s)-1); delete(s,1,pos('.',s)); scan(u); ss:=copy(s,1,pos('(',s)-1); scan(a); delete(s,1,pos('(',s)); delete(s,length(s),1); ss:=s; scan(b); solve; end; close(f1); close(f2); end.
Code mẫu của hieult
import java.math.BigInteger; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String strLine = in.readLine(); int t=Integer.parseInt(strLine); BigInteger x,y,z,d,p,q,e[],f[]; e=new BigInteger[22]; f=new BigInteger[22]; e[1]=BigInteger.valueOf(9);f[1]=BigInteger.valueOf(10); for(int i=2;i<=20;i++) { e[i]=e[i-1].multiply(BigInteger.valueOf(10)); e[i]=e[i].add(BigInteger.valueOf(9)); f[i]=f[i-1].multiply(BigInteger.valueOf(10)); } while(t>0) { t--; strLine = in.readLine(); int i=0; Integer m=0,n=0; x=y=z=BigInteger.ZERO; while(strLine.charAt(i)!='.') { x=x.multiply(BigInteger.valueOf(10)); x=x.add(BigInteger.valueOf(strLine.charAt(i)-'0')); i++; } i++; if(strLine.charAt(i)!='(') { m=0; while(strLine.charAt(i)!='(') { y=y.multiply(BigInteger.valueOf(10)); y=y.add(BigInteger.valueOf(strLine.charAt(i)-'0')); m++; i++; } i++; n=0; while(strLine.charAt(i)!=')') { z=z.multiply(BigInteger.valueOf(10)); z=z.add(BigInteger.valueOf(strLine.charAt(i)-'0')); n++; i++; } p=y.multiply(e[n]);p=p.add(z); q=e[n].multiply(f[m]); x=x.multiply(q); p=p.add(x); } else { i++; n=0; while(strLine.charAt(i)!=')') { z=z.multiply(BigInteger.valueOf(10)); z=z.add(BigInteger.valueOf(strLine.charAt(i)-'0')); n++; i++; } p=z.add(BigInteger.ZERO); q=e[n].add(BigInteger.ZERO); x=x.multiply(q); p=p.add(x); } d=p.gcd(q);p=p.divide(d);q=q.divide(d); System.out.println(p.toString() + "/"+ q.toString()); } } }
Code mẫu của khuc_tuan
import java.io.*; import java.math.*; import java.util.*; public class Main { class Fraction { public BigInteger a, b; public Fraction() { a = BigInteger.ZERO; b = BigInteger.ONE; } public Fraction(BigInteger a, BigInteger b) { this.a = a; this.b = b; } void toigian() { BigInteger uc = a.gcd(b); // System.out.println(this.toString()); a = a.divide(uc); b = b.divide(uc); } public Fraction add(Fraction f) { Fraction r = new Fraction( a.multiply(f.b) .add( b.multiply(f.a) ), b.multiply(f.b)); r.toigian(); return r; } public String toString() { return a + "/" + b; } } String go(String s) { Fraction res = new Fraction(); //System.out.println(s); String[] tmp = s.split("[.]"); //System.out.println(tmp.length); res = res.add(new Fraction(new BigInteger(tmp[0]), BigInteger.ONE)); s = tmp[1]; String b = null, c = null; for(int i=0;i<s.length();++i) if(s.charAt(i)=='(') { b = s.substring(0, i); c = s.substring(i+1, s.length()-1); } BigInteger pow10 = BigInteger.ONE; for(int i=0;i<b.length();++i) pow10 = pow10.multiply(BigInteger.TEN); res = res.add( new Fraction((b.equals("") ? BigInteger.ZERO : new BigInteger(b)), pow10)); String s99 = ""; for(int i=0;i<c.length();++i) s99 += "9"; res = res.add(new Fraction(new BigInteger(c), pow10.multiply(new BigInteger(s99)))); return res.toString(); } public static void main(String[] args) throws Exception { BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(kb.readLine()); for(int t=0;t<n;++t) { String s = kb.readLine(); String res = new Main().go(s); System.out.println(res); } } }
Bình luận