码迷,mamicode.com
首页 > 其他好文 > 详细

1088. Rational Arithmetic (20)

时间:2016-05-06 12:34:58      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.


IDEA

1.关键在于化简分数,需要找到两个数a,b的最大公约数comm_divisor()

2.若a是负数,需要输出括号和符号等


CODE

#include<cstdio>
int comm_divisor(long long a,long long b){
	long long min,max;
	min=(a>b)?b:a;
	max=(a>b)?a:b;
	long long r=max%min;
	while(r){
		max=min;
		min=r;
		r=max%min;
	}
	return min;
}
void output(long long a,long long b){
	int flag=0;
	if(a<0){
		a=-a;
		flag=1;
		//printf("fu");
	}
	if(!a){
		printf("0");
	}else{
		long long div=comm_divisor(a,b);
		a/=div;
		b/=div;
		long long c=a/b;
		long long d=a%b;
		if(flag){//负的 
			if(d==0){
				printf("(-%lld)",c);
			}else{
				if(c==0){
					printf("(-%lld/%lld)",a,b);
				}else{
					printf("(-%lld %lld/%lld)",c,d,b);
				}
			} 
		}else{
			if(d==0){
				printf("%lld",c);
			}else{
				if(c==0){
					printf("%lld/%lld",a,b);
				}else{
					printf("%lld %lld/%lld",c,d,b);
				}
			} 
		}
	}
}
void add(long long a1,long long b1,long long a2,long long b2)
{
	output(a1,b1);
	printf(" + ");
	output(a2,b2);
	printf(" = ");
	output(a1*b2+a2*b1,b1*b2);
	printf("\n");
}
void minus(long long a1,long long b1,long long a2,long long b2)
{
	output(a1,b1);
	printf(" - ");
	output(a2,b2);
	printf(" = ");
	output(a1*b2-a2*b1,b1*b2);
	printf("\n");
}
void muilt(long long a1,long long b1,long long a2,long long b2)
{
	output(a1,b1);
	printf(" * ");
	output(a2,b2);
	printf(" = ");
	output(a1*a2,b1*b2);
	printf("\n");
}
void divide(long long a1,long long b1,long long a2,long long b2)
{
	output(a1,b1);
	printf(" / ");
	output(a2,b2);
	printf(" = ");
	if(a2==0) printf("Inf\n");
	else{
		if(a2<0){
			a2=-a2;
			a1=-a1;
		}
		output(a1*b2,b1*a2);
		printf("\n");
	}
}
int main(){
	long long a1,b1,a2,b2;
	scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);
	add(a1,b1,a2,b2);
	minus(a1,b1,a2,b2);
	muilt(a1,b1,a2,b2);
	divide(a1,b1,a2,b2);
	return 0;
}

技术分享

1088. Rational Arithmetic (20)

标签:

原文地址:http://blog.csdn.net/baidu_32157201/article/details/51329001

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!