Decimal to the simplest fraction (HDU 1717)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.