首页 > 试题广场 >

有理数四则运算(20)

[编程题]有理数四则运算(20)
  • 热度指数:9954 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
本题要求编写程序,计算2个有理数的和、差、积、商。

输入描述:
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分
母不为0。


输出描述:
分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的
最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中
没有超过整型范围的整数。
示例1

输入

5/3 0/6

输出

1 2/3 + 0 = 1 2/3<br/>1 2/3 - 0 = 1 2/3<br/>1 2/3 * 0 = 0<br/>1 2/3 / 0 = Inf
推荐
啥头像
总体思路:
    模拟小学有理数的运算,注意约分和输出格式
注意点:
   所有输出均转换为真分式形式
   负号的位置,负号在括号内,不是在括号外。    (-1 1/3)
   整数部分为0时不输出整数部分,真分式部分为0时也不输出

代码如下:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <sstream>

using namespace std;

long long greatestCommonDivisor(long long a, long long b);
string getProperFraction(long long num, long long den);

int main()
{
    // 提取分数的两部分
    string fraction1, fraction2;
    cin >> fraction1 >> fraction2;
    long long num1, den1, num2, den2;
    int nPos = fraction1.find('/');
    num1 = atoi((fraction1.substr(0, nPos)).c_str());
    den1 = atoi((fraction1.substr(nPos+1)).c_str());
    nPos = fraction2.find('/');
    num2 = atoi((fraction2.substr(0, nPos)).c_str());
    den2 = atoi((fraction2.substr(nPos+1)).c_str());

    // 四则运算
    string first = getProperFraction(num1, den1);
    string second = getProperFraction(num2, den2);
        // +
    long long num = num1*den2 + num2*den1;
    long long den = den1*den2;
    string third = getProperFraction(num, den);
    cout << first << " + " << second << " = " << third << endl;
        // -
    num = num1*den2 - num2*den1;
    third = getProperFraction(num, den);
    cout << first << " - " << second << " = " << third << endl;
        // *
    num = num1*num2; den = den1*den2;
    third = getProperFraction(num, den);
    cout << first << " * " << second << " = " << third << endl;
        // /
    num = num1*den2; den = den1*num2;
    if(den == 0) {
        cout << first << " / " << second << " = Inf" << endl;
    } else {
        third = getProperFraction(num, den);
        cout << first << " / " << second << " = " << third << endl;
    }

    return 0;
}

string getProperFraction(long long num, long long den)
{
    // 基本边界、基本变量
    if(num == 0) {
        return "0";
    }
    bool isMinus = false;
    string str = "";
    if(num*den < 0) {
        isMinus = true; num = abs(num);
        den = abs(den); str = "(-";
    }

    //约分
    long long gcd = greatestCommonDivisor(num, den);
    num /= gcd; den /= gcd;
    stringstream ss; ss << (num/den);
    string intPart, fracPart;
    ss >> intPart;
    stringstream ss2; ss2 << (num%den);
    ss2 >> fracPart;
    if((num/den)>0) {
        str += intPart;
    }
    if((num%den)>0) {
        if((num/den)>0) {
            str += " ";
        }
        str += fracPart; str += '/';
        stringstream ss3; string temp;
        ss3 << den; ss3 >> temp;
        str += temp;
    }
    if(isMinus) {
        str += ')';
    }
    return str;
}

long long greatestCommonDivisor(long long a, long long b)
{
    long long c;
    while (b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return a;
} 


编辑于 2016-04-12 22:55:21 回复(1)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String[] a = in.next().split("/");
		String[] b = in.next().split("/");
		int afz = Integer.parseInt(a[0]);
		int afm = Integer.parseInt(a[1]);
		int bfz = Integer.parseInt(b[0]);
		int bfm = Integer.parseInt(b[1]);
		String left = convert(afz, afm);
		String right = convert(bfz, bfm);
		System.out.println(left+" + "+right+" = "+getNum(afz, afm, bfz, bfm, "+"));
		System.out.println(left+" - "+right+" = "+getNum(afz, afm, bfz, bfm, "-"));
		System.out.println(left+" * "+right+" = "+getNum(afz, afm, bfz, bfm, "*"));
		System.out.println(left+" / "+right+" = "+getNum(afz, afm, bfz, bfm, "/"));
	}
	private static String convert(int fz,int fm){
		if(fz==0)
			return "0";
		if(fm==0)
			return "Inf";
		StringBuilder result = new StringBuilder();
		boolean flag = false;
		int k = 0;
		if(fz<0){
			result.append("(-");
			fz = -fz;
			flag = true;
		}
		k = fz/fm;
		fz = fz%fm;
		if(fz==0){
			if(k!=0)
				result.append(k);
			if(!flag)
				return result.toString();
			else
				return result.append(")").toString();
		}
		int gcd = gcd(fz, fm);
		if(gcd!=1){
			fz /= gcd;
			fm /= gcd;
		}
		if(k!=0)
			result.append(k).append(" ");
		result.append(fz).append("/").append(fm);
		if(flag)
			result.append(")");
		return result.toString();
	}
	private static String getNum(int afz,int afm,int bfz,int bfm,String op){
		switch (op) {
		case "+": 
			if(afm==bfm)
				return convert(afz+bfz, afm);
			else{
				int gcd = gcd(afm, bfm);
				int fm = afm*bfm/gcd;
				afz *= fm/afm;
				bfz *= fm/bfm;
				return convert(afz+bfz, fm);
			}
		case "-": 
			if(afm==bfm)
				return convert(afz-bfz, afm);
			else{
				int gcd = gcd(afm, bfm);
				int fm = afm*bfm/gcd;
				afz *= fm/afm;
				bfz *= fm/bfm;
				return convert(afz-bfz, fm);
			}
		case "*": 
			return convert(afz*bfz,afm*bfm);
		case "/": 
			int fm = bfz*afm;
			int fz = afz*bfm;
			if(fm<0){
				fm = -fm;
				fz = -fz;
			}
			return convert(fz,fm);
		default:
			break;
		}
		return null;
	}
	private static int gcd(int a,int b){
		while(a%b!=0){
			int temp = a%b;
			a = b;
			b = temp;
		}
		return b;//最大公因数
	}
}

编辑于 2016-06-11 22:12:32 回复(2)
//
//  main.cpp
//  1034 有理数四则运算 (20分)
//
//  Created by Zide Liu on 2020/1/22.
//  Copyright © 2020 Zide Liu. All rights reserved.
//

#include <iostream>
#include <string>
using namespace std;
long gcd(long a,long b){
    return b?gcd(b,a%b):a;
}
string print(long a,long b){
    long k1=gcd(a,b);
    a=a/k1;b=b/k1;
    if(a==0) return "0";
    string s;
    int judge=0;
    if(b<0){a=-a;b=-b;}
    if(a<0) {s.append("(-");a=-a;judge=1;}
    if(a>=b) s.append(to_string(a/b));
    if(a>=b&&a%b) s.insert(s.end(),' ');
    a=a%b;
    if(a){
        s.append(to_string(a));
        s.insert(s.end(),'/');
        s.append(to_string(b));
    }
    if(judge) s.insert(s.end(),')');
    return s;
}
int main(){
    long a,b,c,d;
    scanf("%ld/%ld %ld/%ld",&a,&b,&c,&d);
    cout<<print(a,b)<<" + "<<print(c,d)<<" = "<<print(b*c+a*d,b*d)<<endl;
    cout<<print(a,b)<<" - "<<print(c,d)<<" = "<<print(a*d-b*c,b*d)<<endl;
    cout<<print(a,b)<<" * "<<print(c,d)<<" = "<<print(a*c,b*d)<<endl;
    b*c?cout<<print(a,b)<<" / "<<print(c,d)<<" = "<<print(a*d,b*c)<<endl:cout<<print(a,b)<<" / "<<print(c,d)<<" = Inf"<<endl;
    return 0;
}
应该很好理解
发表于 2020-01-22 10:48:16 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int relative(int, int);
 
int main()
{
    int a1, b1, a2, b2, tmp, tmp_b1, tmp_b2;
    int integer_a1, integer_a2, other_a1, other_a2;
    int result_a, result_b, integer_result,other_result;
    scanf("%d/%d%d/%d", &a1, &b1, &a2, &b2);
    integer_a1 = a1/b1;other_a1 = a1 - b1 * integer_a1;
    integer_a2 = a2/b2;other_a2 = a2 - b2 * integer_a2;
    tmp_b1 = b1;       tmp_b2 = b2;
 
    if(other_a1)
    {
        tmp = relative(other_a1, b1);
        while(tmp != 1)
        {
            other_a1 /= tmp;
            b1 /= tmp;
            tmp = relative(other_a1, b1);
        }
    }
    if(other_a2)
    {
        tmp = relative(other_a2, b2);
        while(tmp != 1)
        {
            other_a2 /= tmp;
            b2 /= tmp;
            tmp = relative(other_a2, b2);
        }
    }
    //加法
    result_a = a1 * tmp_b2 + a2 * tmp_b1;
    result_b = tmp_b1 * tmp_b2;
    if(result_a)
    {
        tmp = relative(result_a, result_b);
        while(tmp != 1)
        {
            result_a /= tmp;
            result_b /= tmp;
            tmp = relative(result_a, result_b);
        }
    }
    integer_result = result_a / result_b;
    other_result   = result_a - integer_result * result_b;
    if(other_a1 < 0 && integer_a1)
        printf("(%d %d/%d)", integer_a1, -other_a1, b1);
    else if(other_a1 > 0 && integer_a1)
        printf("%d %d/%d", integer_a1, other_a1, b1);
    else if(integer_a1 == 0 && other_a1 < 0)
        printf("(%d/%d)", other_a1, b1);
    else if(integer_a1 == 0 && other_a1 > 0)
        printf("%d/%d", other_a1, b1);
    else if(integer_a1 < 0)
        printf("(%d)", integer_a1);
    else
        printf("%d", integer_a1);
    printf(" + ");
    if(other_a2 < 0 && integer_a2)
        printf("(%d %d/%d)", integer_a2, -other_a2, b2);
    else if(other_a2 > 0 && integer_a2)
        printf("%d %d/%d", integer_a2, other_a1, b2);
    else if(integer_a2 == 0 && other_a2 < 0)
        printf("(%d/%d)", other_a2, b2);
    else if(integer_a2 == 0 && other_a2 > 0)
        printf("%d/%d", other_a2, b2);
    else if(integer_a2 < 0)
        printf("(%d)", integer_a2);
    else
        printf("%d", integer_a2);
    printf(" = ");
    if(other_result < 0 && integer_result)
        printf("(%d %d/%d)\n", integer_result, -other_result, result_b);
    else if(other_result > 0 && integer_result)
        printf("%d %d/%d\n", integer_result, other_result, result_b);
    else if(integer_result == 0 && other_result < 0)
        printf("(%d/%d)\n", other_result, result_b);
    else if(integer_result == 0 && other_result > 0)
        printf("%d/%d\n", other_result, result_b);
    else if(integer_result < 0)
        printf("(%d)\n", integer_result);
    else
        printf("%d\n", integer_result);
 
    //减法
    result_a = a1 * tmp_b2 - a2 * tmp_b1;
    result_b = tmp_b1 * tmp_b2;
    tmp = relative(result_a, result_b);
    if(result_a)
    {
        tmp = relative(result_a, result_b);
        while(tmp != 1)
        {
            result_a /= tmp;
            result_b /= tmp;
            tmp = relative(result_a, result_b);
        }
    }
    integer_result = result_a / result_b;
    other_result   = result_a - integer_result * result_b;
    if(other_a1 < 0 && integer_a1)
        printf("(%d %d/%d)", integer_a1, -other_a1, b1);
    else if(other_a1 > 0 && integer_a1)
        printf("%d %d/%d", integer_a1, other_a1, b1);
    else if(integer_a1 == 0 && other_a1 < 0)
        printf("(%d/%d)", other_a1, b1);
    else if(integer_a1 == 0 && other_a1 > 0)
        printf("%d/%d", other_a1, b1);
    else if(integer_a1 < 0)
        printf("(%d)", integer_a1);
    else
        printf("%d", integer_a1);
    printf(" - ");
    if(other_a2 < 0 && integer_a2)
        printf("(%d %d/%d)", integer_a2, -other_a2, b2);
    else if(other_a2 > 0 && integer_a2)
        printf("%d %d/%d", integer_a2, other_a1, b2);
    else if(integer_a2 == 0 && other_a2 < 0)
        printf("(%d/%d)", other_a2, b2);
    else if(integer_a2 == 0 && other_a2 > 0)
        printf("%d/%d", other_a2, b2);
    else if(integer_a2 < 0)
        printf("(%d)", integer_a2);
    else
        printf("%d", integer_a2);
    printf(" = ");
    if(other_result < 0 && integer_result)
        printf("(%d %d/%d)\n", integer_result, -other_result, result_b);
    else if(other_result > 0 && integer_result)
        printf("%d %d/%d\n", integer_result, other_result, result_b);
    else if(integer_result == 0 && other_result < 0)
        printf("(%d/%d)\n", other_result, result_b);
    else if(integer_result == 0 && other_result > 0)
        printf("%d/%d\n", other_result, result_b);
    else if(integer_result < 0)
        printf("(%d)\n", integer_result);
    else
        printf("%d\n", integer_result);
 
    //乘法
    result_a = a1 * a2;
    result_b = tmp_b1 * tmp_b2;
    tmp = relative(result_a, result_b);
    if(result_a)
    {
        tmp = relative(result_a, result_b);
        while(tmp != 1)
        {
            result_a /= tmp;
            result_b /= tmp;
            tmp = relative(result_a, result_b);
        }
    }
    integer_result = result_a / result_b;
    other_result   = result_a - integer_result * result_b;
    if(other_a1 < 0 && integer_a1)
        printf("(%d %d/%d)", integer_a1, -other_a1, b1);
    else if(other_a1 > 0 && integer_a1)
        printf("%d %d/%d", integer_a1, other_a1, b1);
    else if(integer_a1 == 0 && other_a1 < 0)
        printf("(%d/%d)", other_a1, b1);
    else if(integer_a1 == 0 && other_a1 > 0)
        printf("%d/%d", other_a1, b1);
    else if(integer_a1 < 0)
        printf("(%d)", integer_a1);
    else
        printf("%d", integer_a1);
    printf(" * ");
    if(other_a2 < 0 && integer_a2)
        printf("(%d %d/%d)", integer_a2, -other_a2, b2);
    else if(other_a2 > 0 && integer_a2)
        printf("%d %d/%d", integer_a2, other_a1, b2);
    else if(integer_a2 == 0 && other_a2 < 0)
        printf("(%d/%d)", other_a2, b2);
    else if(integer_a2 == 0 && other_a2 > 0)
        printf("%d/%d", other_a2, b2);
    else if(integer_a2 < 0)
        printf("(%d)", integer_a2);
    else
        printf("%d", integer_a2);
    printf(" = ");
    if(other_result < 0 && integer_result)
        printf("(%d %d/%d)\n", integer_result, -other_result, result_b);
    else if(other_result > 0 && integer_result)
        printf("%d %d/%d\n", integer_result, other_result, result_b);
    else if(integer_result == 0 && other_result < 0)
        printf("(%d/%d)\n", other_result, result_b);
    else if(integer_result == 0 && other_result > 0)
        printf("%d/%d\n", other_result, result_b);
    else if(integer_result < 0)
        printf("(%d)\n", integer_result);
    else
        printf("%d\n", integer_result);
 
    //除法
    result_a = a1 * tmp_b2;
    result_b = tmp_b1 * a2;
    if(result_b < 0)
    {
        result_a = -result_a;
        result_b = -result_b;
    }
    if(result_b == 0)
    {
        if(other_a1 < 0 && integer_a1)
            printf("(%d %d/%d)", integer_a1, -other_a1, b1);
        else if(other_a1 > 0 && integer_a1)
            printf("%d %d/%d", integer_a1, other_a1, b1);
        else if(integer_a1 == 0 && other_a1 < 0)
            printf("(%d/%d)", other_a1, b1);
        else if(integer_a1 == 0 && other_a1 > 0)
            printf("%d/%d", other_a1, b1);
        else if(integer_a1 < 0)
            printf("(%d)", integer_a1);
        else
            printf("%d", integer_a1);
        printf(" / ");
        if(other_a2 < 0 && integer_a2)
            printf("(%d %d/%d)", integer_a2, -other_a2, b2);
        else if(other_a2 > 0 && integer_a2)
            printf("%d %d/%d", integer_a2, other_a1, b2);
        else if(integer_a2 == 0 && other_a2 < 0)
            printf("(%d/%d)", other_a2, b2);
        else if(integer_a2 == 0 && other_a2 > 0)
            printf("%d/%d", other_a2, b2);
        else if(integer_a2 < 0)
            printf("(%d)", integer_a2);
        else
            printf("%d", integer_a2);
        printf(" = Inf\n");
        exit(0);
    }
    tmp = relative(result_a, result_b);
    if(result_a)
    {
        tmp = relative(result_a, result_b);
        while(tmp != 1)
        {
            result_a /= tmp;
            result_b /= tmp;
            tmp = relative(result_a, result_b);
        }
    }
    integer_result = result_a / result_b;
    other_result   = result_a - integer_result * result_b;
    if(other_a1 < 0 && integer_a1)
        printf("(%d %d/%d)", integer_a1, -other_a1, b1);
    else if(other_a1 > 0 && integer_a1)
        printf("%d %d/%d", integer_a1, other_a1, b1);
    else if(integer_a1 == 0 && other_a1 < 0)
        printf("(%d/%d)", other_a1, b1);
    else if(integer_a1 == 0 && other_a1 > 0)
        printf("%d/%d", other_a1, b1);
    else if(integer_a1 < 0)
        printf("(%d)", integer_a1);
    else
        printf("%d", integer_a1);
    printf(" / ");
    if(other_a2 < 0 && integer_a2)
        printf("(%d %d/%d)", integer_a2, -other_a2, b2);
    else if(other_a2 > 0 && integer_a2)
        printf("%d %d/%d", integer_a2, other_a1, b2);
    else if(integer_a2 == 0 && other_a2 < 0)
        printf("(%d/%d)", other_a2, b2);
    else if(integer_a2 == 0 && other_a2 > 0)
        printf("%d/%d", other_a2, b2);
    else if(integer_a2 < 0)
        printf("(%d)", integer_a2);
    else
        printf("%d", integer_a2);
    printf(" = ");
    if(other_result < 0 && integer_result)
        printf("(%d %d/%d)\n", integer_result, -other_result, result_b);
    else if(other_result > 0 && integer_result)
        printf("%d %d/%d\n", integer_result, other_result, result_b);
    else if(integer_result == 0 && other_result < 0)
        printf("(%d/%d)\n", other_result, result_b);
    else if(integer_result == 0 && other_result > 0)
        printf("%d/%d\n", other_result, result_b);
    else if(integer_result < 0)
        printf("(%d)\n", integer_result);
    else
        printf("%d\n", integer_result);
 
    return 0;
}
 
int relative(int a, int b)
{
    a = abs(a);b = abs(b);
    for(int i = 2; i <= a; i++)
    {
        if(a % i == 0 && b % i == 0)
            return i;
    }
    return 1;
}

emmmmmmmmmm总之写完加法之后就从头到尾都在复制粘贴复制粘贴
编辑于 2020-03-06 21:32:44 回复(2)
做的有点麻烦,不过结构应该比较清楚,这种题不想去改了😫
#include <stdio.h>
#include <stdlib.h>
int gcd(int a,int b);
void simple(int fenzi,int fenmu){ //化简
    if(fenzi%fenmu==0){
        if(fenzi<0){
            printf("(%d)",fenzi/fenmu);
        }
        else  printf("%d",fenzi/fenmu);
    }
    else if(abs(fenzi)<fenmu){
        if(fenzi<0) printf("(%d/%d)",fenzi/gcd(abs(fenzi),fenmu),fenmu/gcd(abs(fenzi),fenmu));
        else printf("%d/%d",fenzi/gcd(abs(fenzi),fenmu),fenmu/gcd(abs(fenzi),fenmu));
    }
    else{
        if(fenzi<0) printf("(%d %d/%d)",fenzi/fenmu,(-1)*(fenzi%fenmu)/gcd(abs(fenzi),fenmu),fenmu/gcd(abs(fenzi),fenmu));
        else printf("%d %d/%d",fenzi/fenmu,(fenzi%fenmu)/gcd(abs(fenzi),fenmu),fenmu/gcd(abs(fenzi),fenmu));
    }
}
int gcd(int a,int b){
    if(b==0) return a;
    else{
        return gcd(b,a%b);
    }
}
int lcm(int a,int b){
    return a*b/gcd(a,b);
}
void add(int fenzi1,int fenmu1,int fenzi2,int fenmu2){
    int fenzi,fenmu;
    fenzi=lcm(fenmu1,fenmu2)/fenmu1*fenzi1+lcm(fenmu1,fenmu2)/fenmu2*fenzi2;
    fenmu=lcm(fenmu1,fenmu2);
    simple(fenzi,fenmu);
}
void minus(int fenzi1,int fenmu1,int fenzi2,int fenmu2){
    int fenzi,fenmu;
    fenzi=lcm(fenmu1,fenmu2)/fenmu1*fenzi1-lcm(fenmu1,fenmu2)/fenmu2*fenzi2;
    fenmu=lcm(fenmu1,fenmu2);
    simple(fenzi,fenmu);
}
void multiple(int fenzi1,int fenmu1,int fenzi2,int fenmu2){
    int fenzi,fenmu;
    fenzi=fenzi1*fenzi2;
    fenmu=fenmu1*fenmu2;
    simple(fenzi,fenmu);
}
void divide(int fenzi1,int fenmu1,int fenzi2,int fenmu2){
    int fenzi,fenmu;
    fenzi=fenzi1*fenmu2;
    fenmu=fenmu1*fenzi2;//负号可能跑到分母去
    if(fenmu<0){
        fenmu*=-1;
        fenzi*=-1;
    }
    simple(fenzi,fenmu);
}
int main(){
    int fenzi1,fenmu1,fenzi2,fenmu2;
    scanf("%d/%d %d/%d",&fenzi1,&fenmu1,&fenzi2,&fenmu2);
    simple(fenzi1,fenmu1);//+
    printf(" + ");
    simple(fenzi2,fenmu2);
    printf(" = ");
    add(fenzi1,fenmu1,fenzi2,fenmu2);
    printf("\n");
    simple(fenzi1,fenmu1);//-
    printf(" - ");
    simple(fenzi2,fenmu2);
    printf(" = ");
    minus(fenzi1,fenmu1,fenzi2,fenmu2);
    printf("\n");
    simple(fenzi1,fenmu1);//*
    printf(" * ");
    simple(fenzi2,fenmu2);
    printf(" = ");
    multiple(fenzi1,fenmu1,fenzi2,fenmu2);
    printf("\n");
    simple(fenzi1,fenmu1);///
    printf(" / ");
    simple(fenzi2,fenmu2);
    printf(" = ");
    if(fenzi2==0) printf("Inf");
    else divide(fenzi1,fenmu1,fenzi2,fenmu2);
    printf("\n");
    return 0;
}

编辑于 2020-03-04 14:11:01 回复(0)
最讨厌这种模拟题了,代码冗长冗长的,也不想优化了,过了就行。😫
import java.util.*;

public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        String[] s1 = sc.next().split("/");
        String[] s2 = sc.next().split("/");
        int [] a = {Integer.parseInt(s1[0]), Integer.parseInt(s1[1])};
        int [] b = {Integer.parseInt(s2[0]), Integer.parseInt(s2[1])};
        yueFen(a);
        yueFen(b);
        int [] he = he(a, b);
        int [] cha = cha(a, b);
        int [] ji = ji(a, b);
        int [] shang = shang(a, b); 
        yueFen(a);
        yueFen(b);
        yueFen(he);
        yueFen(cha);
        yueFen(ji);
        yueFen(shang);
        int [] ha = handle(a);
        int [] hb = handle(b);
        int [] hhe = handle(he);
        int [] hcha = handle(cha);
        int [] hji = handle(ji);
        int [] hshang = handle(shang);       
        String str1 = exchange(ha);
        String str2 = exchange(hb);
        System.out.println(str1 + " + " + str2 + " = " + exchange(hhe));
        System.out.println(str1 + " - " + str2 + " = " + exchange(hcha));
        if(str2.length() == 1 && str2.equals("0")){
        	System.out.println(str1 + " * " + str2 + " = " + "0");
        	System.out.println(str1 + " / " + str2 + " = " + "Inf");
        }else {
        	System.out.println(str1 + " * " + str2 + " = " + exchange(hji));
        	System.out.println(str1 + " / " + str2 + " = " + exchange(hshang));
        }
    }
	private static String exchange(int[] a) {
		String s = "";
		if(a[1] == 1 && a[2] == 1)return "1";
		if(a[1] == 0 ){
			if(a[0] == 0)return "0";
			s += a[0];
			if(a[0] < 0) s = "(" + s + ")";
			return s;
		}
		if(a[0] != 0) s += Math.abs(a[0]) + " ";
		s += Math.abs(a[1]) + "/" + Math.abs(a[2]);
		if(a[0] < 0 || a[1] < 0 || a[2] < 0){
			s = "(-" + s + ")";
		}
		return s;
	}
	private static int[] handle(int[] a) {
		int [] ha = {0, a[0], a[1]};
		if(Math.abs(a[0]) > Math.abs(a[1])){
			ha[0] = a[0] / a[1];
			ha[1] = a[0] % a[1];
		}
		return ha;
	}
	private static int[] shang(int[] a, int[] b) {
		int [] res = {0, 0};
		if(a[0] == 0) return res;
		res[0] = a[0] * b[1];
		res[1] = a[1] * b[0];
 		return res;
	}
    private static int[] ji(int[] a, int[] b) {
		int [] res = {0, 0};
		if(a[0] == 0 || b[0] ==0) return res;
		res[0] = a[0] * b[0];
		res[1] = a[1] * b[1];
		return res;
	}
	private static int[] cha(int[] a, int[] b) {
		int [] res = {0, 0};
		if(a[0] == 0 ){
			res[0] = -1 * b[0];
			res[1] = b[1];
			return res;
		}
		if(b[0] == 0)return a;
		res[0] = a[0] - b[0];
		res[1] = a[1];
		return res;
	}
	private static int[] he(int[] a, int[] b) {
		int [] res = {0, 0};
		if(a[0] == 0 )return b;
		if(b[0] == 0)return a;
		int k = 0;
		if(a[1] != b[1]){
			k = a[1];
			a[1] *= b[1];
			a[0] *= b[1];
			b[1] = a[1];
			b[0] *= k;
		}
		res[0] = a[0] + b[0];
		res[1] = a[1];
		return res;
	}
	private static void yueFen(int[] arr) {
		if(arr[0] == 0){
			arr[1] = 0;
			return ;
		}
		if(arr[0] == arr[1]){ 
			arr[0] = arr[1] = 1;
			return ;
		}
		int i = 2;
		while(i <= Math.abs(arr[0])){
			if(arr[0] % i == 0 && arr[1] % i == 0){
				arr[0] /= i;
				arr[1] /= i;
				i = 1;
			}
			i++;
		}
	}
}

发表于 2019-08-20 20:35:01 回复(2)
def conven(a,b):         #将分母分子转换成标准形式(k a/b)
    divNum = simplify(abs(a),b)
    a //= divNum
    b //= divNum
    if a == 0:
        return '0'
    elif a < 0:
        a = abs(a)
        if a < b:
            return '(-%d/%d)' % (a, b)
        c,d = divmod(a,b)
        if d == 0:
            return '(-%d)' % (c)
        return '(-%d %d/%d)' % (c, d, b)
    elif a >= b:
        c,d = divmod(a,b)
        if d == 0:
            return '%d' % (c)
        return '%d %d/%d'%(c,d,b)
    else:
        return '%d/%d' % (a,b)
def simplify(a,b):           #求最大公因子
    while b: 
        a,b = b,a%b
    return a

while True:
    try:
        a,b = input().split()
        a = list(map(int,a.split('/')))
        b = list(map(int,b.split('/')))
        leftA = conven(a[0],a[1])
        leftB = conven(b[0],b[1])
        print('%s + %s = %s' % (leftA,leftB,conven(a[0]*b[1]+b[0]*a[1],a[1]*b[1])))
        print('%s - %s = %s' % (leftA,leftB,conven(a[0]*b[1]-b[0]*a[1],a[1]*b[1])))
        print('%s * %s = %s' % (leftA,leftB,conven(a[0]*b[0],a[1]*b[1])))
        if b[0] == 0:
            print('%s / %s = %s' % (leftA,leftB,'Inf'))
        else:
            print('%s / %s = %s' % (leftA,leftB,conven(a[0]*b[1],a[1]*b[0])))
    except Exception:
        break
编辑于 2018-10-14 00:51:43 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <math.h>

using namespace std;

//最大公因数
long divisor(long a, long b)
{

    while (b ^= a ^= b ^= a %= b);
    return a;

}

//最小公倍数
long multiple(long b1, long b2)
{
    long minMultiple = b1 * b2;
    long maxDivisor = divisor(b1, b2);
    return minMultiple / maxDivisor;
}

int main()
{
    //输入a1/b1,a2/b2,并分离出来换成整数
    long a1, b1, a2, b2;
    char op[4] = { '+', '-', '*', '/' };
    char num1[50], num2[50];
    cin.sync_with_stdio(false);
    cin >> num1 >> num2;
    char *substr1 = strtok(num1, "/");
    a1 = atoi(substr1);
    b1 = atoi(strtok(NULL, "/"));
    char *substr2 = strtok(num2, "/");
    a2 = atoi(substr2);
    b2 = atoi(strtok(NULL, "/"));





    //求a1,b1;a2,b2最大公约数 并化简至最简分数
    long maxDivisor[2];
    maxDivisor[0] = 0 == a1 ? b1 : divisor(abs(a1), b1);        // 0/x 化简成0/1
    maxDivisor[1] = 0 == a2 ? b2 : divisor(abs(a2), b2);
    a1 /= maxDivisor[0];        //化简
    b1 /= maxDivisor[0];
    a2 /= maxDivisor[1];
    b2 /= maxDivisor[1];
    long minMultiple = multiple(b1, b2);        //求b1,b2最小公倍数
    long multiples[2];
    multiples[0] = minMultiple / b1;
    multiples[1] = minMultiple / b2;

    //求x/y最简形式(x,y的最大公约数)   * /有问题
    long c[4], d[4];
    bool flag = false;
    c[0] = multiples[0] * a1 + multiples[1] * a2;
    c[1] = multiples[0] * a1 - multiples[1] * a2;
    c[2] = a1 * a2;
    c[3] = a1 * b2;
    long beforeC[4], maxDivisorC[4];
    for (int i = 0; i < 4; i++)
    {
        if (1 >= i)
        {
            beforeC[i] = c[i] / minMultiple;
            maxDivisorC[i] = divisor(abs(c[i]), minMultiple);
            d[i] = minMultiple / maxDivisorC[i];
            c[i] /= maxDivisorC[i];
            c[i] %= d[i];
        }
        else if (2 == i)
        {
            d[i] = b1 * b2;
            beforeC[i] = c[i] / d[i];
            maxDivisorC[i] = divisor(abs(c[i]), abs(d[i]));
            d[i] /= maxDivisorC[i];
            c[i] /= maxDivisorC[i];
            c[i] %= d[i];
        }
        else if (3 == i)
        {
            d[i] = b1 * a2;
            if (0 == d[i])
            {
                flag = true;
                break;
            }
            beforeC[i] = c[i] / d[i];
            maxDivisorC[i] = divisor(abs(c[i]), abs(d[i]));
            d[i] /= maxDivisorC[i];
            c[i] /= maxDivisorC[i];
            c[i] %= abs(d[i]);
            c[i] = 0 > d[i] ? -c[i] : c[i];
            d[i] = abs(d[i]);
        }
    }

    bool a1flag = 0 > a1 ? true : false;
    bool a2flag = 0 > a2 ? true : false;

    //需要优化
    for (int i = 0; i < 4; i++)
    {
        if (0 == a1)
            cout << 0 << " " << op[i] << " ";
        else
        {
            if (a1flag)
                cout << "(-";
            if (0 != a1 / b1)
                cout << abs(a1 / b1);
            if (a1flag)
            {
                if ((-a1) % b1 != 0)
                {
                    if (0 != a1 / b1)
                        cout << " ";
                    cout << (-a1) % b1 << "/" << b1 << ") " << op[i] << " ";
                }
                else
                    cout << ") " << op[i] << " ";
            }
            else
            {
                if (a1 % b1 != 0)
                {
                    if (0 != a1 / b1)
                        cout << " ";
                    cout << a1 % b1 << "/" << b1 << " " << op[i] << " ";
                }
                else
                    cout << " " << op[i] << " ";
            }
        }

        if (0 == a2)
            cout << 0 << " = ";
        else
        {
            if (a2flag)
                cout << "(-";
            if (0 != a2 / b2)
                cout << abs(a2 / b2);
            if (a2flag)
            {
                if ((-a2) % b2 != 0)
                {
                    if (0 != a2 / b2)
                        cout << " ";
                    cout << (-a2) % b2 << "/" << b2 << ") " << "= ";
                }
                else
                    cout << ") = ";
            }
            else
            {
                if (a2 % b2 != 0)
                {
                    if (0 != a2 / b2)
                        cout << " ";
                    cout << a2 % b2 << "/" << b2 << " = ";
                }
                else
                    cout << " = ";
            }
        }
        if (3 == i && flag)
            cout << "Inf" << endl;
        else if (0 == c[i])
        {
            if (0 > beforeC[i])
                cout << "(" << beforeC[i] << ")" << endl;
            else
                cout << beforeC[i] << endl;
        }
        else
        {
            if (0 < beforeC[i])
                cout << beforeC[i] << " " << c[i] << "/" << d[i] << endl;
            else if (0 == beforeC[i])
            {
                if (0 > c[i])
                    cout << "(" << c[i] << "/" << d[i] << ")" << endl;
                else
                    cout << c[i] << "/" << d[i] << endl;
            }
            else if (0 > beforeC[i])
                cout << "(" << beforeC[i] << " " << abs(c[i]) << "/" << d[i] << ")" << endl;
        }
    }



    return 0;
}

编辑于 2018-07-07 18:14:48 回复(0)
傻瓜式暴力解法,呵呵呵

#include<iostream>
using namespace std;
int gcd(int a,int b);


int main(){
    int a1,b1,a2,b2;
    int res[4];
    char temp;
    int temp1,temp2;
    cin>>a1>>temp>>b1;
    cin>>a2>>temp>>b2;
    //1.加法
    res[0]=a1*b2+a2*b1;
    if(a1==b1)
        cout<<"1 + ";
    else{
        if(a1<0){
            if(-a1>b1){
                cout<<"("<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<") + ";
                else
                    cout<<") + ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<"("<<a1/temp1<<"/"<<b1/temp1<<") + ";
                else
                    cout<<"("<<a1<<"/"<<b1<<") + ";
            }
        }
        else if(a1==0){
            cout<<a1<<" + ";
        }
        else{
            if(a1>b1){
                cout<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<" + ";
                else
                    cout<<" + ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<a1/temp1<<"/"<<b1/temp1<<" + ";
                else
                    cout<<a1<<"/"<<b1<<" + ";
            }
        }
    }
    if(a2==b2)
        cout<<"1 = ";
    else{
        if(a2<0){
            if(-a2>b2){
                cout<<"("<<a2/b2;
                if(a2%b2!=0)
                    cout<<" "<<a2%b2<<"/"<<b2<<") = ";
                else
                    cout<<") = ";
            }
            else{
                temp1=gcd(a2,b2);
                if(temp1>1)
                    cout<<"("<<a2/temp1<<"/"<<b2/temp1<<") = ";
                else
                    cout<<"("<<a2<<"/"<<b2<<") = ";
            }
        }
        else if(a2==0){
            cout<<a2<<" = ";
        }
        else{
            if(a2>b2){
                cout<<a2/b2;
                if(a2%b2!=0)
                    cout<<" "<<a2%b2<<"/"<<b2<<" = ";
                else
                    cout<<" = ";
            }
            else{
                temp1=gcd(a2,b2);
                if(temp1>1)
                    cout<<a2/temp1<<"/"<<b2/temp1<<" = ";
                else
                    cout<<a2<<"/"<<b2<<" = ";
            }
        }
    }
    temp2=b1*b2;
    temp1=gcd(res[0],temp2);
    if(temp1<=0)
        temp1=-temp1;
    res[0]/=temp1;
    temp2/=temp1;
    if(res[0]>0){
        if(res[0]>temp2){
            cout<<res[0]/temp2;
            if(res[0]%temp2==0)
                cout<<endl;
            else
                cout<<" "<<res[0]%temp2<<"/"<<temp2<<endl;
        }
        else
            cout<<res[0]<<"/"<<temp2<<endl;
    }
    else{
        if(-res[0]>temp2){
            cout<<"("<<res[0]/temp2;
            if(res[0]%temp2==0)
                cout<<")"<<endl;
            else
                cout<<" "<<-res[0]%temp2<<"/"<<temp2<<")"<<endl;
        }
        else
            cout<<"("<<res[0]<<"/"<<temp2<<")"<<endl;
    }
    
    //2.
    res[1]=a1*b2-a2*b1;
    if(a1==b1)
        cout<<"1 - ";
    else{
        if(a1<0){
            if(-a1>b1){
                cout<<"("<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<") - ";
                else
                    cout<<") - ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<"("<<a1/temp1<<"/"<<b1/temp1<<") - ";
                else
                    cout<<"("<<a1<<"/"<<b1<<") - ";
            }
        }
        else if(a1==0){
            cout<<a1<<" - ";
        }
        else{
            if(a1>b1){
                cout<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<" - ";
                else
                    cout<<" - ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<a1/temp1<<"/"<<b1/temp1<<" - ";
                else
                    cout<<a1<<"/"<<b1<<" - ";
            }
        }
    }
    if(a2==b2)
        cout<<"1 = ";
    else{
        if(a2<0){
            if(-a2>b2){
                cout<<"("<<a2/b2;
                if(a2%b2!=0)
                    cout<<" "<<a2%b2<<"/"<<b2<<") = ";
                else
                    cout<<") = ";
            }
            else{
                temp1=gcd(a2,b2);
                if(temp1>1)
                    cout<<"("<<a2/temp1<<"/"<<b2/temp1<<") = ";
                else
                    cout<<"("<<a2<<"/"<<b2<<") = ";
            }
        }
        else if(a2==0){
            cout<<a2<<" = ";
        }
        else{
            if(a2>b2){
                cout<<a2/b2;
                if(a2%b2!=0)
                    cout<<" "<<a2%b2<<"/"<<b2<<" = ";
                else
                    cout<<" = ";
            }
            else{
                temp1=gcd(a2,b2);
                if(temp1>1)
                    cout<<a2/temp1<<"/"<<b2/temp1<<" = ";
                else
                    cout<<a2<<"/"<<b2<<" = ";
            }
        }
    }
    temp2=b1*b2;
    temp1=gcd(res[1],temp2);
    if(temp1<=0)
        temp1=-temp1;
    res[1]/=temp1;
    temp2/=temp1;
    if(res[1]>0){
        if(res[1]>temp2){
            cout<<res[1]/temp2;
            if(res[1]%temp2==0)
                cout<<endl;
            else
                cout<<" "<<res[1]%temp2<<"/"<<temp2<<endl;
        }
        else
            cout<<res[1]<<"/"<<temp2<<endl;
    }
    else if(res[1]==0)
        cout<<"0"<<endl;
    else{
        if(-res[1]>temp2){
            cout<<"("<<res[1]/temp2;
            if(res[1]%temp2==0)
                cout<<")"<<endl;
            else
                cout<<" "<<-res[1]%temp2<<"/"<<temp2<<")"<<endl;
        }
        else
            cout<<"("<<res[1]<<"/"<<temp2<<")"<<endl;
    }
    
    //3.
    res[2]=a1*a2;
    if(a1==b1)
        cout<<"1 * ";
    else{
        if(a1<0){
            if(-a1>b1){
                cout<<"("<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<") * ";
                else
                    cout<<") * ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<"("<<a1/temp1<<"/"<<b1/temp1<<") * ";
                else
                    cout<<"("<<a1<<"/"<<b1<<") * ";
            }
        }
        else if(a1==0){
            cout<<a1<<" * ";
        }
        else{
            if(a1>b1){
                cout<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<" * ";
                else
                    cout<<" * ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<a1/temp1<<"/"<<b1/temp1<<" * ";
                else
                    cout<<a1<<"/"<<b1<<" * ";
            }
        }
    }
    if(a2==b2)
        cout<<"1 = ";
    else{
        if(a2<0){
            if(-a2>b2){
                cout<<"("<<a2/b2;
                if(a2%b2!=0)
                    cout<<" "<<a2%b2<<"/"<<b2<<") = ";
                else
                    cout<<") = ";
            }
            else{
                temp1=gcd(a2,b2);
                if(temp1>1)
                    cout<<"("<<a2/temp1<<"/"<<b2/temp1<<") = ";
                else
                    cout<<"("<<a2<<"/"<<b2<<") = ";
            }
        }
        else if(a2==0){
            cout<<a2<<" = ";
        }
        else{
            if(a2>b2){
                cout<<a2/b2;
                if(a2%b2!=0)
                    cout<<" "<<a2%b2<<"/"<<b2<<" = ";
                else
                    cout<<" = ";
            }
            else{
                temp1=gcd(a2,b2);
                if(temp1>1)
                    cout<<a2/temp1<<"/"<<b2/temp1<<" = ";
                else
                    cout<<a2<<"/"<<b2<<" = ";
            }
        }
    }
    temp2=b1*b2;
    temp1=gcd(res[2],temp2);
    if(temp1<=0)
        temp1=-temp1;
    res[2]/=temp1;
    temp2/=temp1;
    if(res[2]==temp2)
        cout<<"1"<<endl;
    else{
        if(res[2]>0){
            if(res[2]>temp2){
                cout<<res[2]/temp2;
                if(res[2]%temp2==0)
                    cout<<endl;
                else
                    cout<<" "<<res[2]%temp2<<"/"<<temp2<<endl;
            }
            else
                cout<<res[2]<<"/"<<temp2<<endl;
        }
        else if(res[2]==0){
            cout<<"0"<<endl;
        }
        else{
            if(-res[2]>temp2){
                cout<<"("<<res[2]/temp2;
                if(res[2]%temp2==0)
                    cout<<")"<<endl;
                else
                    cout<<" "<<-res[2]%temp2<<"/"<<temp2<<")"<<endl;
            }
            else
                cout<<"("<<res[2]<<"/"<<temp2<<")"<<endl;
        }
    }
    
    //4.
    res[3]=a1*b2;
    temp2=b1*a2;
    if(temp2<0){
        res[3]=-res[3];
        temp2=-temp2;
    }
    if(a1==b1)
        cout<<"1 / ";
    else{
        if(a1<0){
            if(-a1>b1){
                cout<<"("<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<") / ";
                else
                    cout<<") / ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<"("<<a1/temp1<<"/"<<b1/temp1<<") / ";
                else
                    cout<<"("<<a1<<"/"<<b1<<") / ";
            }
        }
        else if(a1==0){
            cout<<a1<<" / ";
        }
        else{
            if(a1>b1){
                cout<<a1/b1;
                if(a1%b1!=0)
                    cout<<" "<<a1%b1<<"/"<<b1<<" / ";
                else
                    cout<<" / ";
            }
            else{
                temp1=gcd(a1,b1);
                if(temp1>1)
                    cout<<a1/temp1<<"/"<<b1/temp1<<" / ";
                else
                    cout<<a1<<"/"<<b1<<" / ";
            }
        }
    }
    if(a2==0){
        cout<<a2<<" = Inf"<<endl;
    }
    else{
        if(a2==b2)
            cout<<"1 = ";
        else{
            if(a2<0){
                if(-a2>b2){
                    cout<<"("<<a2/b2;
                    if(a2%b2!=0)
                        cout<<" "<<a2%b2<<"/"<<b2<<") = ";
                    else
                        cout<<") = ";
                }
                else{
                    temp1=gcd(a2,b2);
                    if(temp1>1)
                        cout<<"("<<a2/temp1<<"/"<<b2/temp1<<") = ";
                    else
                        cout<<"("<<a2<<"/"<<b2<<") = ";
                }
            }
            else{
                if(a2>b2){
                    cout<<a2/b2;
                    if(a2%b2!=0)
                        cout<<" "<<a2%b2<<"/"<<b2<<" = ";
                    else
                        cout<<" = ";
                }
                else{
                    temp1=gcd(a2,b2);
                    if(temp1>1)
                        cout<<a2/temp1<<"/"<<b2/temp1<<" = ";
                    else
                        cout<<a2<<"/"<<b2<<" = ";
                }
            }
        }
        temp1=gcd(res[3],temp2);
        if(temp1<=0)
            temp1=-temp1;
        res[3]/=temp1;
        temp2/=temp1;
        if(res[3]==temp2)
            cout<<"1"<<endl;
        else{
            if(res[3]>0){
                if(res[3]>temp2){
                    cout<<res[3]/temp2;
                    if(res[3]%temp2==0)
                        cout<<endl;
                    else
                        cout<<" "<<res[3]%temp2<<"/"<<temp2<<endl;
                }
                else
                    cout<<res[3]<<"/"<<temp2<<endl;
            }
            else if(res[3]==0)
                cout<<"0"<<endl;
            else{
                if(-res[3]>temp2){
                    cout<<"("<<res[3]/temp2;
                    if(res[3]%temp2==0)
                        cout<<")"<<endl;
                    else
                        cout<<" "<<-res[3]%temp2<<"/"<<temp2<<")"<<endl;
                }
                else
                    cout<<"("<<res[3]<<"/"<<temp2<<")"<<endl;
            }
        }
    }
    
    return 0;
}

int gcd(int a,int b){
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}


发表于 2018-03-04 00:23:08 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
     
    private static String euclideanFunction(int numerator, int denominator) {
        int a = numerator;
        int b = denominator;
         
        boolean negative = false;
         
        boolean hasInteger = false;
         
        //去除负号
        if(a < 0 && b < 0) {
            a -= 2 * a;
            b -= 2 * b;
        }else if(a < 0) {
            negative = true;
            a -= 2 * a;
        }else if(b < 0) {
            negative = true;
            b -= 2 * b;
        }
         
        String result = "";
         
        if(b == 0) {
            result = "Inf";
        }else if(a == 0) {
            result = "0";
        }else {
            int c = 0;
            //求最大公约数
            while((a % b) != 0) {
                c = a % b;
                a = b;
                b = c;
            }

            //数字不能化作整数则进行约分
            if(c != 0) {
                a = numerator;
                b = denominator;
                 
                //去除负号
                if(a < 0 && b < 0) {
                    a -= 2 * a;
                    b -= 2 * b;
                }else if(a < 0) {
                    a -= 2 * a;
                }else if(b < 0) {
                    b -= 2 * b;
                }
                 
                a /= c;
                b /= c;
            }
             
            if(negative) {
                result += "(-";
            }
            //整数部分
            if((a / b) > 0) {
                result += (a / b);
                hasInteger = true;
            }
            //分数部分
            if((a % b) > 0) {
                if(hasInteger) {
                    result += " ";
                }
                result += (a % b) + "/" + b;
            }
             
            if(negative) {
                result += ")";
            }
        }
        return result;
    }
     
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
 
        String[] numbers = bufferedReader.readLine().split(" ");
        String[] number1 = numbers[0].split("/");
        String[] number2 = numbers[1].split("/");
         
        int a1 = Integer.parseInt(number1[0]);
        int a2 = Integer.parseInt(number1[1]);
         
        int b1 = Integer.parseInt(number2[0]);
        int b2 = Integer.parseInt(number2[1]);
         
        String simpleNumber1 = euclideanFunction(a1, a2);
        String simpleNumber2 = euclideanFunction(b1, b2);
         
        System.out.println(simpleNumber1 + " + " + simpleNumber2 +
                " = " + euclideanFunction(a1 * b2 + a2 * b1, a2 * b2));
        System.out.println(simpleNumber1 + " - " + simpleNumber2 +
                " = " + euclideanFunction(a1 * b2 - a2 * b1, a2 * b2));
        System.out.println(simpleNumber1 + " * " + simpleNumber2 +
                " = " + euclideanFunction(a1 * b1, a2 * b2));
        System.out.println(simpleNumber1 + " / " + simpleNumber2 +
                " = " + euclideanFunction(a1 * b2, a2 * b1));
    }
}

编辑于 2017-11-22 17:37:23 回复(0)
#include<cstdio>

using namespace std;

long long gcd(long long a,long long b){
	return b==0?a:gcd(b,a%b); 
}
/*
-4/2 -5/3
0 -2  mod
-2 -1 div
*/
void tranform(long long a,long long b){
	int q=0,r=0;
	long long g = gcd(a,b);
	if(a!=0&&b!=0){
		if(g<0) g = -g;
		if(g!=0){
			a = a/g;
			b = b/g;
		}
	}
	if(b == 0){
		printf("Inf");
		return; 
	}
	//b>0 
	if(a>0){//a>0 
		if(a<b){
			printf("%lld/%lld",a,b);
		}else{
			q = a/b;//肯定不为0 
			r = a%b; 
			if(r == 0){
				printf("%d",q); 
			}else{//肯定大于0
				printf("%d %d/%lld",q,r,b);
			} 	
		}
	}else{
		if(a==0){
			printf("0");
		}else{//a<0,b>0
			if(a > -b){
				printf("(%lld/%lld)",a,b);	
			}else{
				q = a/b;//可能为0 或小于0 
				r = a%b;//可能为0 或小于0 
				if(r==0){
					printf("(%d)",q);
				}else{
					printf("(%d %d/%lld)",q,-r,b);
				}	
			}
		}
	}
}
int main(){
	int a1,b1,a2,b2;
	long long a,b;
	scanf("%d/%d %d/%d",&a1,&b1,&a2,&b2);
	/////////////+///////////
	tranform(a1,b1);
	printf(" + ");
	tranform(a2,b2);
	printf(" = ");
	
	long long x1 = gcd(a1*b2,b1*b2);
	long long x2 = gcd(a2*b1,b1*b2);
	long long g =gcd(x1,x2); 
    if(g!=0){
    	if(g<0) g = -g;
    	b = (b1*b2)/g;
		a = (long long)(a1*b2)/g+(a2*b1)/g;
    }
	tranform(a,b);
	printf("\n");
	//printf("%d %d")
	/////////////-///////////
	tranform(a1,b1);
	printf(" - ");
	tranform(a2,b2);
	printf(" = ");
	a = a1*b2-a2*b1;
	b = b1*b2;
	tranform(a,b);
	printf("\n");
	/////////////*///////////
	tranform(a1,b1);
	printf(" * ");
	tranform(a2,b2);
	printf(" = ");
	a = a1*a2;
	b = b1*b2;
	tranform(a,b);
	printf("\n");
	///////////// "/"///////////
	tranform(a1,b1);
	printf(" / ");
	tranform(a2,b2);
	printf(" = ");
	if(a2 == 0){
		printf("Inf");
	}else{
		a = a1*b2;
		b = b1*a2;
		if(b<0){
			a = -a;
			b = -b;
		}
		tranform(a,b);
	}

	/*int a,b;
	while(scanf("%d/%d",&a,&b)==2){
		tranform(a,b);
	}*/ 
	return 0;
}
先附上代码,这个代码在牛客上通过了,但是PAT上的第三个测试点怎么调都没有过,
一直没有找到原因。啊,快奔溃了。

编辑于 2017-06-22 20:32:28 回复(2)
#include<iostream>
#include<cmath>
using namespace std;
void yf1(int *p1,int *p2);//对 a1/b1    a2/b2 约分处理
void yf(int a,int b);//对计算结果先约分,后输出
void print(int a,int b);
int main() 
{
int a1,a2,b1,b2,c1,c2;
char tmp;//留着存放数字间的符号
cin>>a1>>tmp>>b1>>a2>>tmp>>b2;
//约分
yf1(&a1,&b1);
yf1(&a2,&b2);
//"+"
print(a1,b1);
cout<<" + ";
print(a2,b2);
c1 = (a1 * b2) + (a2 * b1);
c2 = b1 * b2;
cout<<" = ";
yf(c1,c2);
cout<<endl;
//"-"
print(a1,b1);
cout<<" - ";
print(a2,b2);
c1 = (a1 * b2) - (a2 * b1);
c2 = b1 * b2;
cout<<" = ";
yf(c1,c2);
cout<<endl;
//"*"
print(a1,b1);
cout<<" * ";
print(a2,b2);
c1 = a1 * a2;
c2 = b1 * b2;
cout<<" = ";
yf(c1,c2);
cout<<endl;
//"/"
print(a1,b1);
cout<<" / ";
print(a2,b2);
c1 = a1 * b2;
c2 = b1 * a2;
        //输出时 分母不能小于0,所以当分母小于0 时,处理
if(c2 < 0){
c1 *= -1;
c2 *= -1;
}
cout<<" = ";
if(c2){  //分母是否为0
yf(c1,c2);
}else{
cout<<"Inf";
}
cout<<endl;
return 0;
}
void yf1(int *p1,int *p2)
{
int flag = 1;
int a = *p1,b = *p2;
if(a<0){
flag = -1;
}
a = abs(a);
for(int i=a;i>1;i--){
if((a%i==0)&&(b%i==0)){
a = a/i;
b = b/i;
}
}
a = a*flag;
*p1 = a;
*p2 = b;
}
void yf(int a,int b){
int flag = 1;
if(a<0){
flag = -1;
}
a = abs(a);
for(int i=a;i>1;i--){
if((a%i==0)&&(b%i==0)){
a = a/i;
b = b/i;
}
}
a = a*flag;
print(a,b);
}
void print(int a, int b)
{
int tmp = 0,flag = 0;;
if(0 == a){//分子为0,输出0; 
cout<<0;
return;
}
if(a<0){
flag = 1;
}
if(abs(a) >= b){
tmp = a/b;
a = abs(a)%b;
if(flag){//a<0
cout<<"(";
}
if(tmp){//有整部
cout<<tmp;
if(a){// 且 a 不等于 0 
cout<<" ";
}
}
if(a){// a 不等于 0 
cout<<a<<"/"<<b;
}
if(flag){
cout<<")";
}
}
发表于 2016-11-03 10:39:31 回复(0)
#include<stdio.h>
int gcd(int a, int b){
	int r;
	if(a<b){r=a;a=b;b=r;}
	r=a%b;
	while(r){a=b;b=r;r=a%b;}
	return b;
}
void stdPrint(int a, int b, int signId){
	int r,k;
	if(signId)
	   printf("(-");
    if(a==0)
       printf("0");
    else{
   	   r=gcd(a,b);
 	   a/=r; b/=r;
  	   if(b==1)
	      printf("%d",a);
       else{
 	      k=a/b;
   	 	  if(k)
 	 	    printf("%d %d/%d",k,a%b,b);
 	      else
            printf("%d/%d",a,b);}
        }
     if(signId)
       printf(")");		
}
void Operator(int a1,int b1, int signId1, int a2, int b2, int signId2, char c){
	 int temp1,temp2,signId=0;
	 stdPrint(a1,b1,signId1);
	 printf(" %c ",c);
	 stdPrint(a2,b2,signId2);
	 printf(" = ");
	 if(signId1) a1*=-1;
     if(signId2) a2*=-1;
	 switch(c){
 		case '+':
           temp1=a1*b2+a2*b1;
           temp2=b1*b2;
           if(temp1<0){temp1*=-1;signId=1;}
		   stdPrint(temp1,temp2,signId);
		   break;
		case '-':
		    temp1=a1*b2-a2*b1;
			temp2=b1*b2;
			if(temp1<0){temp1*=-1;signId=1;}
			stdPrint(temp1,temp2,signId);
			break;
		case '*':
		     temp1=a1*a2;
			 temp2=b1*b2;
			 if(temp1<0){temp1*=-1;signId=1;}
			 stdPrint(temp1,temp2,signId);
			 break;
		case '/':
		     if(a2){
   			    temp1=a1*b2;
				temp2=b1*a2;
				if(temp1*temp2<0){signId=1;}
				if(temp1<0) temp1*=-1;
				if(temp2<0) temp2*=-1;
				stdPrint(temp1,temp2,signId);  
     		 }else
     		    printf("Inf");
  		     break;}
         printf("\n");
}


int main(){
	int a1,b1,signId1,a2,b2,signId2;
	char c;
	a1=0;signId1=0;
	scanf("%c",&c);
	if(c=='-') signId1=1;
	else a1=c-'0';
	scanf("%c",&c);
	while(c!='/'){
		a1=10*a1+(c-'0');
		scanf("%c",&c);
	}
	b1=0;
	scanf("%c",&c);
	while(c!=' '){
		b1=10*b1+(c-'0');
		scanf("%c",&c);
	}
	a2=0;signId2=0;
	scanf("%c",&c);
	if(c=='-') signId2=1;
	else a2=c-'0';
	scanf("%c",&c);
	while(c!='/'){
		a2=10*a2+(c-'0');
		scanf("%c",&c);
	}
	b2=0;
	scanf("%c",&c);
	while(c!='\n'){
		b2=10*b2+(c-'0');
		scanf("%c",&c);
	}

	Operator(a1,b1,signId1,a2,b2,signId2,'+');
	Operator(a1,b1,signId1,a2,b2,signId2,'-');
	Operator(a1,b1,signId1,a2,b2,signId2,'*');
        Operator(a1,b1,signId1,a2,b2,signId2,'/');
}

发表于 2016-09-05 22:35:25 回复(0)
#include <cstdio>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

string changeStr(int num1, int num2) {
    int g = gcd(num1, num2);
    num1 /= g;
    num2 /= g;
    if (num1 == 0) {
        return "0";
    }
    string result;
    int a = abs(num1);
    int b = abs(num2);
    if (a % b == 0) {
        result.append(to_string(a / b));
    } else if (a / b > 0) {
        result.append(to_string(a / b)).append(" ").append(to_string(a % b)).append("/").append(
                to_string(b));
    } else {
        result.append(to_string(a % b)).append("/").append(
                to_string(b));
    }
    if (num1 * num2 < 0) {
        result = string("(-").append(result).append(")");
    }
    return result;
}

string compute(int a1, int a2, int b1, int b2, char op) {
    string result;
    int down = a2 * b2;
    int up1 = a1 * b2;
    int up2 = b1 * a2;

    string title = changeStr(a1, a2);
    title.append(" ").push_back(op);
    title.push_back(' ');
    title.append(changeStr(b1, b2));
    title.append(" = ");
    switch (op) {
        case '+': {
            result = changeStr(up1 + up2, down);
            break;
        }
        case '-': {
            result = changeStr(up1 - up2, down);
            break;
        }
        case '*': {
            result = changeStr(up1 * up2, down * down);
            break;
        }
        case '/': {
            result = up2 == 0 ? "Inf" : changeStr(up1 * down, down * up2);
            break;
        }
    }
    return title.append(result);
}

int main() {
    int a1, a2, b1, b2;
    scanf("%d/%d %d/%d", &a1, &a2, &b1, &b2);
    cout << compute(a1, a2, b1, b2, '+') << endl;
    cout << compute(a1, a2, b1, b2, '-') << endl;
    cout << compute(a1, a2, b1, b2, '*') << endl;
    cout << compute(a1, a2, b1, b2, '/') << endl;
}

发表于 2020-02-01 23:57:35 回复(0)
#include <iostream>
#include <math.h>
using namespace std;
struct fraction{                                            //分数
    long long up,down;
};
long long gcd(long long a,long long b){                     //求最大公约数
    if(b==0) return a;
    else return gcd(b,a%b);
}
fraction reduction(fraction result){                        //化简
    if(result.down<0){                                      //使分母非负
        result.up=-result.up;
        result.down=-result.down;
    }
    if(result.up==0) result.down=1;
    else{
        long long d=gcd(abs(result.up),abs(result.down));   //约分
        result.up/=d;result.down/=d;
    }
    return result;
}
fraction add(fraction a,fraction b){                        //分数加法运算
    fraction result;
    result.up=a.up*b.down+b.up*a.down;
    result.down=a.down*b.down;
    return result;
}
fraction minu(fraction a,fraction b){
    fraction result;
    result.up=a.up*b.down-b.up*a.down;
    result.down=a.down*b.down;
    return result;
}
fraction multi(fraction a,fraction b){
    fraction result;
    result.up=a.up*b.up;
    result.down=a.down*b.down;
    return result;
}
fraction divide(fraction a,fraction b){
    fraction result;
    result.up=a.up*b.down;
    result.down=a.down*b.up;
    return result;
}
void printresult(fraction a){                               //输出
    a=reduction(a);
    if(a.up<0) cout<<"(";
    if(a.down==1) cout<<a.up;  //整数                        
    else if(abs(a.up)>a.down)  //假分数化为带分数
        cout<<a.up/a.down<<" "<<abs(a.up)%a.down<<"/"<<a.down;
    else cout<<a.up<<"/"<<a.down; //真分数
    if(a.up<0) cout<<")";
}
int main(){
    fraction a,b;
    scanf("%lld/%lld %lld/%lld",&a.up,&a.down,&b.up,&b.down);
    printresult(a);cout<<" + ";printresult(b);cout<<" = ";printresult(add(a,b));cout<<'\n';
    printresult(a);cout<<" - ";printresult(b);cout<<" = ";printresult(minu(a,b));cout<<'\n';
    printresult(a);cout<<" * ";printresult(b);cout<<" = ";printresult(multi(a,b));cout<<'\n';
    printresult(a);cout<<" / ";printresult(b);cout<<" = ";
    if(b.up==0) cout<<"Inf"<<'\n';
    else{printresult(divide(a,b));cout<<'\n';}    
    return 0;
}

发表于 2018-02-18 19:51:41 回复(0)
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		String str1=sc.next();
		String str2=sc.next();
		String[] arr1=str1.split("/");
		String[] arr2=str2.split("/");
		int a1=Integer.parseInt(arr1[0]);
		int b1=Integer.parseInt(arr1[1]);
		int a2=Integer.parseInt(arr2[0]);
		int b2=Integer.parseInt(arr2[1]);
		//System.out.println(a1+" "+b1+" "+a2+" "+b2);
		//
		String num1=myFunc(a1,b1);
		String num2=myFunc(a2,b2);
		String res1=myFunc1(a1,b1,a2,b2,'+');
		String res2=myFunc1(a1,b1,a2,b2,'-');
		String res3=myFunc2(a1,b1,a2,b2,'*');
		String res4=myFunc2(a1,b1,a2,b2,'/');
		System.out.println(num1+"+"+num2+"="+res1);
		System.out.println(num1+"-"+num2+"="+res2);
		System.out.println(num1+"*"+num2+"="+res3);
		System.out.print(num1+"/"+num2+"="+res4);
	}
	public static int getZuiDaGYS(int a,int b){
		if(a<b){
			int temp=a;
			a=b;
			b=temp;
		}
		int res=0;
		if(b!=0){
			if(a%b==0){
				res=b;
			}else{
				for(int i=(int)b/2;i>=2;i--){
					if(a%i==0&&b%i==0){
						res=i;
						break;
					}
				}
			}
		}
		
		return res;
	}
	public static String myFunc(int a,int b){
		StringBuffer res=new StringBuffer();
		
		if(a<0){
			res.insert(0, "(-)");
		}
		a=Math.abs(a);
		int zdgys=getZuiDaGYS(a,b);
		if(zdgys>0){
			a=a/zdgys;
			b=b/zdgys;
		}
		String str="";
		int i=a/b;
		int j=a%b;
		if(j==0){
			
			str+=i;
		}else if(i>0){
			str+=i+" "+j+"/"+b;
		}else{
			str+=a+"/"+b;
		}
		if(res.length()>0){
			res.insert(2, str);
			str=res.toString();
		}
		return str;
		 
	}
	public static String myFunc1(int a1,int b1,int a2,int b2,char c){
		//加法
		int a3=0;
		int b3=b1*b2;
		if(c=='+'){
			a3=a1*b2+a2*b1;
		}else{
			a3=a1*b2-a2*b1;
		}
		//System.out.println(a3+" "+b3);
		return myFunc(a3,b3);
		
	}
	public static String myFunc2(int a1,int b1,int a2,int b2,char c){
		//加法
		String res=null;
		if(a2==0){
			if(c=='*'){
				res="0";
			}else{
				res="Inf";
			}
		}else{
			int a3,b3;
			if(c=='*'){
				a3=a1*a2;
				b3=b1*b2;
			}else{
				if(a2>0){
					a3=a1*b2;
					b3=b1*a2;
				}else{
					a3=-1*a1*b2;
					b3=-1*b1*a2;
				}
				
			}
			res=myFunc(a3,b3);
		}
		return res;
	}
}


发表于 2015-09-21 19:53:15 回复(0)
#include<stdio.h>
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
void p(int a,int b){
    if(b==0){printf("Inf");return;}
    int f,t;
    b>0?:(a*=-1,b*=-1),f=a<0?-1:1,a *=f,t=gcd(a,b),a/=t,b/=t;
    a%b?a>b?printf(f>0?"%d %d/%d":"(-%d %d/%d)",a/b,a%b,b):printf(f>0?"%d/%d":"(-%d/%d)",a,b):printf(!a||f>0?"%d":"(-%d)",a/b);
}
int main (){//the shorter,the better.
    int a1,b1,a2,b2;
    for(;~scanf("%d/%d %d/%d",&a1,&b1,&a2,&b2);p(a1,b1),printf(" + "),p(a2,b2),printf(" = "),p(a1*b2+a2*b1,b1*b2),printf("\n"),p(a1,b1),printf(" - "),p(a2,b2),printf(" = "),p(a1*b2-a2*b1,b1*b2),printf("\n"),p(a1,b1),printf(" * "),p(a2,b2),printf(" = "),p(a1*a2,b1*b2),printf("\n"),p(a1,b1),printf(" / "),p(a2,b2),printf(" = "),p(a1*b2,a2*b1),printf("\n"));
}

编辑于 2018-01-30 16:32:24 回复(4)
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
void youli(int a,int b){
    int flag=1;
    if(b==0){
        printf("Inf");
    	return;
    }
    if(a==0){
    	printf("0");
    	return;
    }
    if(a*b<0){
         flag=0;
    }
    if(a<0)
        a=-a;
    if(b<0)
        b=-b;
    int common=gcd(a,b);
    a=a/common;b=b/common;
    int j=a/b;a=a%b;
    if(!flag){
    if(j){
    	if(a!=0)
    		printf("(-%d %d/%d)",j,a,b);
    	else
    		printf("(-%d)",j);
    	
    }
    else 
        printf("(-%d/%d)",a,b);
    	
    }else{
    	if(j){
        if(a!=0)
    		printf("%d %d/%d",j,a,b);
    	else
    		printf("%d",j);
    		
    	}
    else 
        printf("%d/%d",a,b);
    }
}
int main(){
    int a,b,c,d;
    ~scanf("%d/%d %d/%d",&a,&b,&c,&d);
    youli(a,b);cout<<" + ";youli(c,d);cout<<" = ";youli(a*d+c*b,b*d);cout<<endl;
    youli(a,b);cout<<" - ";youli(c,d);cout<<" = ";youli(a*d-c*b,b*d);cout<<endl;
    youli(a,b);cout<<" * ";youli(c,d);cout<<" = ";youli(a*c,b*d);cout<<endl;
    youli(a,b);cout<<" / ";youli(c,d);cout<<" = ";youli(a*d,b*c);cout<<endl;
}

发表于 2021-06-12 17:20:06 回复(0)
#include<iostream>
#include<stdlib.h>
using namespace std;
int gcd(int a, int b)
{
    return a%b ? gcd(b,a%b) : b;
}
void simplemost(int a,int b)
{
    int flag=0,fa;
    if(a==0)
    {
        cout<<"0";
        return;
    }
    else 
    {
        if(a<0)
        {
            cout<<"(-";
            flag=1;
        }
        a=abs(a);
        if(gcd(a,b)!=1)
        {
            fa=a/gcd(a,b);
            b=b/gcd(a,b);
            a=fa;
        }
        if(a>b && a%b!=0)
            cout<<a/b<<" "<<a%b<<"/"<<b;
        else if(a%b==0)
            cout<<a/b;
        else
            cout<<a<<"/"<<b;
        if(flag)
            cout<<")";
    }
}
int main()
{
    int a1,b1,a2,b2;
    scanf("%d/%d %d/%d",&a1,&b1,&a2,&b2);
    //firest line
    simplemost(a1,b1);
    cout<<" + ";
    simplemost(a2,b2);
    cout<<" = ";
    simplemost(a1*b2+a2*b1,b1*b2);
    cout<<endl;
    
    //second line
    simplemost(a1,b1);
    cout<<" - ";
    simplemost(a2,b2);
    cout<<" = ";
    simplemost(a1*b2-a2*b1,b1*b2);
    cout<<endl;
    
    //third line
    simplemost(a1,b1);
    cout<<" * ";
    simplemost(a2,b2);
    cout<<" = ";
    simplemost(a1*a2,b1*b2);
    cout<<endl;
    
    //fourth line
    simplemost(a1,b1);
    cout<<" / ";
    simplemost(a2,b2);
    cout<<" = ";
    if(a2==0)
        cout<<"Inf";
    else if(b1*a2<0)
        simplemost(-a1*b2,-b1*a2);
    else
        simplemost(a1*b2,b1*a2);
    cout<<endl;
    return 0;
}

发表于 2021-03-12 18:47:43 回复(0)
import math
def f(st):
    return [int(i) for i in st.split('/')]
class fractional:
    def __init__(self,a,b):
        self.a=a
        self.b=b
        
    def __add__(self,other):
        a=self.a*other.b+self.b*other.a
        b=self.b*other.b
        return fractional(a,b)
        
    def __sub__(self,other):
        a=self.a*other.b-self.b*other.a
        b=self.b*other.b
        return fractional(a,b)
    
    def __mul__(self,other):
        a=self.a*other.a
        b=self.b*other.b
        return fractional(a,b)
    
    def __truediv__(self,other):
        a=self.a*other.b
        b=self.b*other.a
        return fractional(a,b)
    
    def simple(self):
        a=self.a
        b=self.b
        r=math.gcd(a,b)
        flag=0 if a*b>0 else 1
        a=abs(a//r)
        b=abs(b//r)
        if not a:
            return '0'
        if not b:
            return 'Inf'
        k=a//b
        a=a%b
        return flag*'(-'+(bool(k)*'{0}'+bool(k and a)*' '+bool(a)*'{1}/{2}').format(k,a,b)+flag*')'
        
        
A,B=[fractional(*f(i)) for i in input().split()]
spA,spB=A.simple(),B.simple()
for op in ['+','-','*','/']:
    print(spA,op,spB,'=',eval('A'+op+'B').simple())
    

发表于 2020-08-24 16:32:38 回复(0)
在pat官网数值必须定义为long,否则相乘会溢出
发表于 2020-07-22 21:59:34 回复(0)