poj1930

不知道无限循环小数怎么化分数的直接百度

知道直接暴力枚举循环的位数即可

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<assert.h>
#include<ctime>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<sstream>
#include<stack>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define me(s)  memset(s,0,sizeof(s))
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define pf printf
#define sf scanf
#define Di(x) int x;scanf("%d",&x)
#define in(x) inp(x)
#define in2(x,y) inp(x),inp(y)
#define in3(x,y,z) inp(x),inp(y),inp(z)
#define ins(x) scanf("%s",x)
#define ind(x) scanf("%lf",&x)
#define IO ios_base::sync_with_stdio(0);cin.tie(0)
#define READ freopen("C:/Users/ASUS/Desktop/in.txt","r",stdin)
#define WRITE freopen("C:/Users/ASUS/Desktop/out.txt","w",stdout)
template<class T> void inp(T &x) {//读入优化
    char c = getchar(); x = 0;
    for (; (c < 48 || c>57); c = getchar());
    for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; }
}
typedef pair <int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-15;
string s;
ull gcd(ull a,ull b) {return b==0?a:gcd(b,a%b);}
ull Pow(int x){
    ull ret=1;
    for(int i=1;i<=x;i++)
    ret*=10;
    return ret;
}
int main(){
    //READ;
    //WRITE;
    while(cin>>s){
        if(s=="0") break;
        s=s.substr(2,s.size()-5);
        //cout<<s<<endl;
        int len=s.size();
        ull n=atoi(s.c_str());
        if(!n) {
            printf("0/1\n");
            continue;
        }
        ull minx=0,miny=inf;
        for(int i=1;i<=len;i++){
            ull x=n-atoi(s.substr(0,len-i).c_str());
            ull y=Pow(len)-Pow(len-i);
            ull d=gcd(x,y);
            x/=d;y/=d;
            if(y<miny) {
               miny=y;
               minx=x;
            }
        }
        cout<<minx<<"/"<<miny<<endl;
    }
   
}

猜你喜欢

转载自www.cnblogs.com/033000-/p/10035878.html