HDU 1717 Fractional fraction analysis:
无限小数可按照小数部分是否循环分成两类:无限循环小数和无限不循环小数。无限不循环小数不能化分数;
consider: How does the fractional number of infinite loops turn into fractions?
For example: 0.325656......x100=32.5656 ... ①
0.325656......x10000=3256.56 ... Ii
Use ②-① to get:
0.325656......x9900=3256.5656 ... -32.5656 ...
0.325656......x9900=3256-32
So, 0.325656......=3224/9900
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <math.h>#include <algorithm>using namespace STD;#define LL Long Long#define CLR (A, B) memset (a,b,sizeof a)Charstr[ A]; ll GCD (ll A, ll b) {returnb==0? A:GCD (b, a%b);}intMain () {#ifndef Online_judgeFreopen ("In.txt","R", stdin); Freopen ("OUT.txt","W", stdout);#endif //Online_judge intTscanf("%d", &t); while(t--) {scanf('%s ', str);intlen=strlen(str); LL Tot,num; tot=0; num=1; LL Xx,yy; xx=0; yy=1;BOOLflag=0; for(intI=1; i<len; i++) {if(str[i]>=' 0 '&&str[i]<=' 9 ') {tot=tot*Ten+ (str[i]-' 0 '); num*=Ten; }if(str[i]==' (') flag=1;if(!flag&&str[i]>=' 0 '&&str[i]<=' 9 ') {xx=xx*Ten+ (str[i]-' 0 '); yy*=Ten; } }if(NUM!=YY) Num-=yy;if(TOT!=XX) tot-=xx;//cout<<tot<< "<<num<<" "<<xx<<" "<<yy<<endl;LL KK=GCD (Tot,num);//cout<<kk<<endl; cout<<tot/kk<<"/"<<num/kk<<endl;//printf ("%d/%d\n", TOT/KK,NUM/KK);}return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Decimal to the simplest fraction (HDU 1717)