POJ 3979 分数加减法

Description

编写一个C程序,实现两个分数的加减法

Input

输入包含多行数据
每行数据是一个字符串,格式是"a/boc/d"。

其中a, b, c, d是一个0-9的整数。o是运算符"+"或者"-"。

数据以EOF结束
输入数据保证合法

Output

对于输入数据的每一行输出两个分数的运算结果。
注意结果应符合书写习惯,没有多余的符号、分子、分母,并且化简至最简分数

Sample Input

1/8+3/8
1/4-1/2
1/3-1/3

Sample Output

1/2
-1/4
0

POJ 3979 分数加减法

方法一:局限性

POJ 3979 分数加减法
 1 import java.util.Scanner;
 2 
 3 //弊端:只能针对分子分母均为个位数的整数
 4 public class Main1 {
 5 
 6     public static void main(String[] args){
 7         Scanner sc=new Scanner(System.in);
 8         String str;
 9         int n1,n2,n3,d1,d2,d3,op,x,y,z;
10         boolean neg=false;
11         while(sc.hasNext()){
12             neg=false;
13             str=sc.next();
14             op=1;
15             n1=str.charAt(0)-‘0‘;
16             d1=str.charAt(2)-‘0‘;
17             if(str.charAt(3)==‘-‘){
18                 op=-1;
19             }
20             n2=str.charAt(4)-‘0‘;
21             d2=str.charAt(6)-‘0‘;
22             n3=n1*d2+op*n2*d1;
23             d3=d1*d2;
24             if(n3==0){
25                 System.out.println("0");
26                 continue;
27             }
28             if(n3<0){
29                 neg=true;
30             }
31             n3=Math.abs(n3);
32             d3=Math.abs(d3);
33             x=n3>d3?n3:d3;
34             y=n3<d3?n3:d3;
35             while(y>0){
36                 z=x%y;
37                 x=y;
38                 y=z;
39             }
40             n3/=x;
41             d3/=x;
42             
43             if(neg){
44                 System.out.println("-");
45             }
46             if(d3==1){
47                 System.out.println(n3);
48             }else{
49                 System.out.println(n3+"/"+d3);
50             }
51         }
52     }
53 }
View Code

方法二:通用性

POJ 3979 分数加减法
  1 import java.math.BigInteger;
  2 import java.util.Scanner;
  3 
  4 
  5 public class Main2 {
  6 
  7     public static void main(String[] args){
  8         Scanner sc=new Scanner(System.in);
  9         String op=null;
 10         while(sc.hasNext()){
 11             String line=sc.nextLine();
 12             int index=line.indexOf("+");
 13             if(index!=-1)
 14                 op="+";
 15             else
 16                 op="-";
 17             String[]data=line.split("/|\\+|-");
 18 
 19             Fraction a=new Fraction();
 20             Fraction b=new Fraction();
 21             Fraction c=new Fraction();
 22             Fraction d=new Fraction();
 23 
 24             a.set(data[0], data[1]);
 25             b.set(data[2], data[3]);
 26             if(op.equals("+"))
 27                 a.add(b).print();
 28             else
 29                 a.subtract(b).print();
 30         }
 31     }
 32 }
 33 
 34 class Fraction{
 35     BigInteger numerator,denominator,gcd;//分子,分母,公约数
 36 
 37     void set(String n,String d){//初始化分子分母
 38         numerator=new BigInteger(n);
 39         denominator=new BigInteger(d);
 40     }
 41 
 42     void simplify(){//化简
 43         gcd=numerator.gcd(denominator);
 44         numerator=numerator.divide(gcd);
 45         denominator=denominator.divide(gcd);
 46         if(denominator.compareTo(BigInteger.ZERO)<0){
 47             numerator=numerator.negate();//对指定的 BigInteger 值求反。
 48             denominator=denominator.negate();
 49         }
 50     }
 51 
 52     int compareTo(Fraction y){
 53 
 54         Fraction z=new Fraction();
 55         return z.numerator.compareTo(BigInteger.ZERO);
 56     }
 57 
 58     Fraction abs(){//绝对值
 59         Fraction z=new Fraction();
 60         if(numerator.compareTo(BigInteger.ZERO)<0){
 61             z.numerator=numerator.negate();
 62         }else{
 63             z.numerator=numerator;
 64         }
 65         if(denominator.compareTo(BigInteger.ZERO)<0){
 66             z.denominator=denominator.negate();
 67         }else{
 68             z.denominator=denominator;
 69         }
 70         return z;
 71     }
 72 
 73     Fraction negate(){//相反值
 74         Fraction z=this;
 75         z.numerator=z.numerator.negate();
 76         return z;
 77     }
 78 
 79     boolean zero(){//判断是否是零
 80         return numerator.compareTo(BigInteger.ZERO)==0;
 81     }
 82 
 83     Fraction add(Fraction y) {//加法运算
 84         Fraction z = new Fraction();
 85         gcd = denominator.gcd(y.denominator);
 86         z.denominator = denominator.multiply(y.denominator).divide(gcd);//分母与分母相乘并化简
 87         z.numerator = numerator.multiply(z.denominator.divide(denominator)).add(y.numerator.multiply(z.denominator.divide(y.denominator)));
 88         z.simplify();
 89         return z;
 90     }
 91 
 92     Fraction subtract(Fraction y) {//减法运算
 93         Fraction z = new Fraction();
 94         gcd = denominator.gcd(y.denominator);
 95         z.denominator = denominator.multiply(y.denominator).divide(gcd);
 96         z.numerator = numerator.multiply(z.denominator.divide(denominator)).subtract(y.numerator.multiply(z.denominator.divide(y.denominator)));
 97         z.simplify();
 98         return z;
 99     }
100 
101     Fraction multiply(Fraction y) {//乘法运算
102         Fraction z = new Fraction();
103         z.numerator = numerator.multiply(y.numerator);
104         z.denominator = denominator.multiply(y.denominator);
105         z.simplify();
106         return z;
107     }
108 
109     Fraction divide(Fraction y) {//除法运算
110         Fraction z = new Fraction();
111         //分子分母交叉相乘
112         z.numerator = numerator.multiply(y.denominator);
113         z.denominator = denominator.multiply(y.numerator);
114         z.simplify();
115         return z;
116     }
117 
118     void print() {//打印输出
119         if (denominator.compareTo(BigInteger.ONE) != 0) {
120             System.out.printf("%s/%s", numerator.toString(), denominator.toString());
121         } else {
122             System.out.printf("%s", numerator.toString());
123         }
124         System.out.println();
125     }
126 }
View Code

POJ 3979 分数加减法

上一篇:Mac配上“刘海屏”?苹果新专利显示Mac将支持Face ID


下一篇:归并排序(C#实现)