hdu 1717 小數化分數
阿新 • • 發佈:2019-01-27
數學
題目分析:會補上的……
code:
#include <iostream> #include <algorithm> #include <string> using namespace std; long long gcd(long long a,long long b) {//引數也要是long long,之前寫成int了 if(a==0)return b; if(b==0)return a; return gcd(b,a%b); } int main() { long long t,a,b,xh,bx,flag,xhm,bxm,g; string c,cxh,cbx; while(cin>>t) { for(int i=1;i<=t;i++) { flag=1; xh=bx=0; cin>>c; c.erase(0,2);//去掉0. cxh.clear(); cbx.clear(); if(c.find('(')!=string::npos)cxh.assign(c,c.find('(')+1,c.find(')')-c.find('(')-1);//迴圈部分 拷到cxh //c.erase(c.find('('),c.find(')')-c.find('('));拷完擦除 cbx.assign(c,0,c.find('('));//非迴圈部分拷入cbx reverse(cxh.begin(),cxh.end());//反轉使低位在前 reverse(cbx.begin(),cbx.end()); //cout<<cxh<<endl<<cbx<<endl; for(int j=0;j<cxh.size();j++) { xh+=flag*(cxh[j]-'0'); flag*=10; //cout<<"&&& "<<xh<<" &&&"<<endl; } xhm=flag-1; //cout<<"迴圈部分先母后子: "<<xhm<<" "<<xh<<endl; flag=1; for(int j=0;j<cbx.size();j++) { bx+=flag*(cbx[j]-'0'); flag*=10; } bxm=flag; //cout<<"非迴圈部分先母后子: "<<bxm<<" "<<bx<<endl; //cout<<"output end"<<endl; xhm+=(!xhm); bxm+=(!bxm); xhm*=bxm; g=gcd(xhm,xh); xhm/=g; xh/=g; g=gcd(bxm,bx); bxm/=g; bx/=g; a=xh*bxm+bx*xhm; b=bxm*xhm; g=gcd(a,b); a/=g; b/=g; //cout<<"Case "<<i<<": "<<a<<'/'<<b<<endl; cout<<a<<'/'<<b<<endl; } } return 0; }
PS:從師哥的steps裡搞來的一道題,得到思想之後程式碼是自己寫的,結果wrong,不知怎麼改就放下了,今天打掃unsolved problem list的時候翻出以前存下的wrong程式碼,只改了第六行那一點,就A了,這是近幾個月最快的一道題O(∩_∩)O哈!