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

需要强调分母不为0吗?到底错在哪呢?

Posted by chen082x at 2011-07-06 14:33:20 on Problem 3979
#include <stdio.h>
int same(int n1,int n2)
{
	int i;
	if(n1>=n2)
	{
		for(i=n2;i>0;i--)
			if(n1%i==0&&n2%i==0)
				return i;
	}
	else
	{
		for(i=n1;i>0;i--)
			if(n1%i==0&&n2%i==0)
				return i;
	}
}
int main()
{
	char o;
	char str[20];
	int a,b,c,d,n1,n2,s,k;
	while(scanf("%s",str)!=EOF)
	{
		a=str[0]-48;
		b=str[2]-48;
		c=str[4]-48;
		d=str[6]-48;
		o=str[3];
		n2=b*d;
		if(o=='+')
			n1=a*d+b*c;
		if(o=='-')
			n1=a*d-b*c;
		if(n1==0)
			printf("0\n");
		if(n1%n2==0)
			printf("%d\n",n1/n2);
		else
		{
			if(n1<0)
				k=-n1;
			else
				k=n1;
			s=same(k,n2);
			n2=n2/s;
			n1=n1/s;
			printf("%d/%d\n",n1,n2);
		}
		
	} 
	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