zoukankan      html  css  js  c++  java
  • PAT-A 1088. Rational Arithmetic

    1088. Rational Arithmetic

    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.

    Sample Input 1:

    2/3 -4/2
    

    Sample Output 1:

    2/3 + (-2) = (-1 1/3)
    2/3 - (-2) = 2 2/3
    2/3 * (-2) = (-1 1/3)
    2/3 / (-2) = (-1/3)
    

    Sample Input 2:

    5/3 0/6
    

    Sample Output 2:

    1 2/3 + 0 = 1 2/3
    1 2/3 - 0 = 1 2/3
    1 2/3 * 0 = 0
    1 2/3 / 0 = Inf
    

    程序代码:

    #include<stdio.h>
    long long divisor(long long a,long long b);
    void print(long long a,long long b);
    void add(long long a,long long b,long long c,long long d);
    void minus(long long a,long long b,long long c,long long  d);
    void division(long long a,long long b,long long c,long long d);
    void multiply(long long a,long long b,long long  c,long long d);
    int main()
    {
    	long long a,b,c,d;
    	scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    	add(a,b,c,d);
    	minus(a,b,c,d);
    	multiply(a,b,c,d);
    	division(a,b,c,d);
    	return 0;
    }
    void add(long long a,long long  b,long long c,long long d)
    {
    	print(a,b);
    	printf(" + ");
    	print(c,d);
    	printf(" = ");
    	print(a*d+b*c,b*d);
    	printf("
    ");	
    }
    void minus(long long a,long long b,long long c,long long  d)
    {
            print(a,b);
            printf(" - ");
            print(c,d);
            printf(" = ");
            print(a*d-b*c,b*d);
            printf("
    ");
    	
    }
    void multiply(long long a,long long b,long long  c,long long d)
    {
            print(a,b);
            printf(" * ");
            print(c,d);
            printf(" = ");
            print(a*c,b*d);
            printf("
    ");
    }
    void division(long long a,long long b,long long c,long long d)
    {
    	long long m= a*d;
    	long long n= b*c;
    	print(a,b);
            printf(" / ");
            print(c,d);
            printf(" = ");
            if(n==0)
    		printf("Inf
    ");
    	else 
    	{
    		if(n<0)
    		{
    			m=m*-1;		//把负号调整到分子上
    			n=n*-1;
    		}
    		print(m,n);
    		printf("
    ");    
    	}
    }
    void print(long long a,long long b)//打印函数
    {
    	long long tmp;
    	int flag =0;
    	if(a==0)
    	{
    		printf("0");
    		return ;
    	}
    	else if(a<0)//不管分子为正还是为负,统统按照正整数处理
    	{
    		a= -a;
    		flag = 1;//标记为负数
    	}
    	tmp = divisor(a,b);//求出最大公约数
    	a=a/tmp;//化为最简形式
    	b=b/tmp;
    	long c=a/b;//整数部分
    	if(flag)
    	{
    		if(b==1)	
    			printf("(-%lld)",a);
    		else if(c>0)	
    			printf("(-%lld %lld/%lld)",c,(a%b),b);
    		else
    			printf("(-%lld/%lld)",a%b,b);
    	}
    	else
    	{
    		if(b==1)
    			printf("%lld",a);			
                    else if(c>0)
                            printf("%lld %lld/%lld",c,a%b,b);
                    else
                            printf("%lld/%lld",a%b,b);
    	}
    }
    long long  divisor(long long a,long long b)//辗转相除法求最大公约数
    {
    	long long max = a>b?a:b;
    	long long min =a<b?a:b;
    	long long  r=max%min;
    	while(r)
    	{
    		max=min;
    		min = r;
    		r = max%min;
    	}	
    	return min;
    }
    
    
  • 相关阅读:
    phpstorm使用svn爆出“cannot load supported formats” 的解决
    本地wamp的Internal Server Error错误解决方法
    mac下apache的多站点配置
    Git 一些错误的解决方法
    【总结整理】登录模块---摘自《人人都是产品经理》
    【总结整理】产品经理优秀品质----《结网》
    【总结整理】传统行业如何合理利用互联网思维----摘自《人人都是产品经理》
    【总结整理】租房产品创业的三个方向和三个产品---摘自《人人都是产品经理》
    【总结整理】KANO 模型
    【总结整理】关于GrowingIO、友盟、google analysis等数据分析
  • 原文地址:https://www.cnblogs.com/zhengkang/p/5770599.html
Copyright © 2011-2022 走看看