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 chenxuan123456789 at 2012-08-02 20:12:54 on Problem 3979
#include <stdio.h>
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}
int main()
{
	int a,b,c,d,m,n,s;
	char op;
	while(scanf("%d/%d%c%d/%d",&a,&b,&op,&c,&d)!=EOF)
	{
		m=b*d/gcd(b,d);
		a=a*(m/b);
		c=c*(m/d);
		switch(op)
		{
		case'+':
			n=a+c;
			s=gcd(m,n);
			if(s==m)
			printf("%d\n",n/s);
			else
			printf("%d/%d\n",n/s,m/s);
			break;
		case'-':
			if(a<c)
			{
			printf("-");
			n=c-a;
			s=gcd(m,n);
			if(m==s)
			printf("%d\n",n/s);
			else
			printf("%d/%d\n",n/s,m/s);
			break;
			}
			else
			if(a==c)
			{
				printf("0\n");
				break;
			}
			else
			{
				n=a-c;
				s=gcd(m,n);
				if(m==s)
				printf("%d\n",n/s);
				else
				printf("%d/%d\n",n/s,m/s);
				break;
			}
		}
	}
	return 1;
}




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