输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分
母不为0。
分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的
最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中
没有超过整型范围的整数。
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
啥
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;//最大公因数
}
}
//
// 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;
}
应该很好理解
#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;
} #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;
} 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++;
}
}
} 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
#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);
}
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));
}
}
#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上的第三个测试点怎么调都没有过,
一直没有找到原因。啊,快奔溃了。
#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,'/');
}
#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;
} #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;
}
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;
}
}
#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"));
}
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using pii = pair<int,int>;
i64 gcd(i64 x,i64 y){
if ( y == 0){
return x;
} else {
return gcd(y,x % y);
}
}
void solve(i64& x,i64& y){
if ( x == 0 || y == 0){
return;
}
while(gcd(x,y) != 1){
i64 g = gcd(x,y);
x /= g;
y /= g;
}
}
string change(i64 m,i64 d){
string s;
if ( m != 0){
if ( m / d != 0){
s += to_string(m / d);
if ( m % d != 0){
s += ' ';
}
}
if ( m % d != 0){
s += to_string(m % d) + '/' + to_string(d);
}
} else{
s = "0";
}
if ( s[0] == '-'){
s = "(" + s + ")";
}
return s;
}
string work(i64 m, i64 d){
string s;
if ( m < 0 && d < 0){
i64 temp = abs(m);
i64 tempd = abs(d);
solve(temp,tempd);
s = change(temp,tempd);
} else if ( m < 0){
i64 temp = abs(m);
solve(temp,d);
s = "-" + change(temp,d);
} else if ( d < 0){
i64 temp = abs(d);
solve(m,temp);
s = "-" + change(m,temp);
} else {
solve(m,d);
s = change(m,d);
}
if ( s[0] == '-'){
s = "(" + s + ")";
}
return s;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
string s1,s2;
cin >> s1 >> s2;
auto sm1 = s1.substr(0,s1.find('/'));
auto sd1 = s1.substr(s1.find('/') + 1);
int m1 = stoi(sm1);
int d1 = stoi(sd1);
auto sm2 = s2.substr(0,s2.find('/'));
auto sd2 = s2.substr(s2.find('/') + 1);
int m2 = stoi(sm2);
int d2 = stoi(sd2);
i64 mm1 = m1;
i64 dd1 = d1;
solve(mm1,dd1);
i64 mm2 = m2;
i64 dd2 = d2;
solve(mm2,dd2);
s1 = change(mm1,dd1);
s2 = change(mm2,dd2);
i64 ans1m = m1 * d2 + m2 * d1;
i64 ans1d = d1 * d2;
auto ans1s = work(ans1m,ans1d);
i64 ans2m = m1 * d2 - m2 * d1;
i64 ans2d = d1 * d2;
auto ans2s = work(ans2m,ans2d);
string ans3s;
if ( m1 == 0 || m2 == 0){
ans3s = "0";
} else {
i64 ans3m = m1 * m2;
i64 ans3d = d1 * d2;
ans3s = work(ans3m,ans3d);
}
string ans4s;
if ( m1 == 0 ){
ans4s = "0";
} else if ( m2 == 0){
ans4s = "Inf";
} else {
i64 ans4m = m1 * d2;
i64 ans4d = m2 * d1;
ans4s = work(ans4m,ans4d);
}
cout << s1 << " + " << s2 << " = " << ans1s << endl;
cout << s1 << " - " << s2 << " = " << ans2s << endl;
cout << s1 << " * " << s2 << " = " << ans3s << endl;
cout << s1 << " / " << s2 << " = " << ans4s << endl;
return 0;
} #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;
} #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;
} 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())