CSU 1303


题意:将无限循环小数转换为最简分数。


#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define inf 100000000
#define INF -0x3f3f3f3f
#define eps 1e-8
#define ll long long
#define N 2020
#define M 50005
ll gcd(ll a,ll b)
{
    if(b==0) return a;
	return gcd(b,a%b);
}
int main()
{
	int i,j,k;
	char s[100];
	while(~scanf("%s",s))
	{
		ll c=0,a=1,b=0,d=0;
		int vis=0;
		for(i=2;i<strlen(s);i++)
		{
		    if(s[i]=='(')
			{
			   vis=1;
			   continue;
			}
			if(s[i]==')') break;
			if(vis)
			{
			   c=c*10+s[i]-'0';
			   d=d*10+9;
			}
			else 
			{
			   a=a*10;
			   b=b*10+s[i]-'0';
			}
		}
		if(d==0)
		{
		   printf("%lld/%lld\n",b/gcd(a,b),a/(gcd(a,b)));
		}
		else
		{
		   printf("%lld/%lld\n",(b*d+c)/gcd(b*d+c,d*a),d*a/gcd(b*d+c,d*a));
		}
	}
	return 0;
}


你可能感兴趣的:(CSU 1303)