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;
    }
    
    
  • 相关阅读:
    新加的keyword编码错误
    Robot framework模拟打开浏览器问题
    Chrome无法登陆
    Android App用MulticastSocket监听组播,为什么连接到不同路由、在不同手机上跑,有的能收到有的收不到
    ubuntu 14.04/15.10 安装基于eclipse的android app开发环境
    ubuntu15.10英文系统中文输入法配置 fcitx
    Amazon S3 上传文件 SSL23_GET_SERVER_HELLO握手错误
    Google Map API v2 番外篇 关于gps位置偏差及修正方法探讨
    Google Map API v2 (四)----- 导航路径
    Google Map API v2 (三)----- 地图上添加标记(Marker),标记info窗口,即指定经纬度获取地址字符串
  • 原文地址:https://www.cnblogs.com/zhengkang/p/5770599.html
Copyright © 2011-2022 走看看