Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:Is the test data OK?

Posted by javaman at 2005-08-26 09:18:28 on Problem 1930
In Reply To:Re:Is the test data OK? Posted by:javaman at 2005-08-26 09:15:50
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>

using namespace std;

typedef __int64 ll;

char s[100];

ll gcd(ll a,ll b) {
	return b?gcd(b,a%b):a;
}

int main() {
ll a,b,c,d,p,q,g,x,bestp,bestq;
int bit,i;

	while (true) {
		scanf("%s",s);
		if (!strcmp(s,"0"))
			break;
		bit=strlen(s)-5;
		s[bit+2]=0;
		sscanf(s+2,"%I64d",&x);
		bestq=-1;
		for (i=1;i<=bit;++i) {
			p=(ll) pow(10.,(double) (bit-i));
			q=(ll) pow(10.,(double) i);
			a=x/q;
			c=x%q;
			b=p;
			d=(q-1)*p;
			g=gcd(a,b);
			a/=g;
			b/=g;
			g=gcd(c,d);
			c/=g;
			d/=g;
			p=a*d+b*c;
			q=b*d;
			g=gcd(p,q);
			p/=g;
			q/=g;
			if ((bestq<0) || (bestq>q) || ((bestq==q) && (bestp<p))) {
				bestp=p;
				bestq=q;
			}
		}
		printf("%I64d/%I64d\n",bestp,bestq);
	}
	return 0;
}




		
	


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator