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

试了无数次了,自己运行各种情况都考虑了啊,怎么还错啊!帮忙看看吧,,,(附代码)

Posted by kelsi at 2011-07-20 22:00:59 on Problem 3979
#include "stdio.h"
#include "math.h"
int GCD(int a,int b)
{
	if(!b)
		return a;
	else
		return GCD(b,a%b);
}
int main()
{
	int gcd,sum;
	char a[10];
   	while(scanf("%s",a)!=EOF)
	{
	    a[0]=(int)a[0]-48;
	    a[2]=(int)a[2]-48;
	    a[4]=(int)a[4]-48;
	    a[6]=(int)a[6]-48;
		if(a[0]>=0&&a[0]<=9&&a[2]>=0&&a[2]<=9&&a[4]>=0
                                           &&a[4]<=9&&a[6]>=0&&a[6]<=9)
		{
			   
			gcd=GCD(a[2],a[6]);
			a[0]=a[0]*a[6]/gcd;
			a[4]=a[4]*a[2]/gcd;
			a[6]=a[2]=a[2]*a[6]/gcd;
			if(a[3]=='+')
			{
				sum=a[0]+a[4];
				gcd=GCD(sum,a[6]);
				sum/=gcd;a[6]/=gcd;
				if(sum==a[6]) {printf("1\n");continue;}
				printf("%d/%d\n",sum,a[6]);
			}
			if(a[3]=='-')
			{
			    sum=a[0]-a[4];
				gcd=GCD(sum,a[6]);
				sum/=gcd;a[6]/=gcd;
				if(sum*a[6]<0)
				{
					
					sum=abs(sum);
					a[6]=abs(a[6]);
					if(sum==a[6]) 
                                              {printf("-1\n");continue;}
				    printf("-%d/%d\n",sum,a[6]);
				}
				else 
				{
                                 if(sum==a[6]) 
                                    {printf("1\n");continue;}
                                 printf("%d/%d\n",sum,a[6]);
                                 }
			}
		}
	}
	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