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

我要哭了,wa了不下几十次了。。。。笨死了。。。哪位大侠帮忙看看哦。。。

Posted by xiaolanhai at 2006-10-22 10:52:24 on Problem 3039
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define E 0.0000000000000001
int gcd(int a,int b)
{
	if (b==0)
		return a;
	else
	{
		return gcd(b,a%b);
	}
}
main()
{
	int n,d;
	int i;
	double min ;
	double data;
	int tmp;
	double tmpdata;
	int tmpn,tmpd;
	min =32768;
	scanf("%d%d",&n,&d);
	/*if (n==d)
	{
		printf("32766 32767\n");
		return 0;
	}
	if ((n==1)&&(d==32767))
	{printf("1 32766\n");
	return 0;}*/

	data = (double)n/d;
	
	for (i=1; i<=32767; i++)
	{
		tmp = (int) floor(i*data);

		tmpdata = fabs((double)(tmp-1)/i-data) ;
		
		if (tmpdata < E)
			tmpdata = 32768.0;
		
		if (tmpdata <= min)
		{
			min = tmpdata;
			tmpn = tmp-1;
			tmpd = i;
		}
		tmpdata = fabs((double)tmp/i-data) ;
		
		if (tmpdata < E)
			tmpdata = 32768.0;
		
		if (tmpdata < min)
		{
			min = tmpdata;
			tmpn = tmp;
			tmpd = i;
		}
		if (tmpdata < E)
			tmpdata = 32768.0;
		tmpdata = fabs((double)(tmp+1)/i- data);
		if (tmpdata <min)
		{
			min = tmpdata;
			tmpn = tmp+1;
			tmpd = i;

		}

	}

	tmp = gcd(tmpn,tmpd);/*求最大公约数*/
	printf("%d %d\n",tmpn/tmp,tmpd/tmp);

	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