# 着急 ！！！！帮帮小白

baidu_32011679 2015-10-14 05:24:33

1 2
1 4

3 4

...全文
264 6 打赏 收藏 转发到动态 举报

6 条回复

li-heng 2015-10-14
• 打赏
• 举报

``````
package test.service.user;

public class Fraction {

/** 分子 */
private int molecular;

/** 分母 */
private int  denominator;

public Fraction(){};
/**
* 构造方法
* @param molecular 分子
* @param denominator 分母
*/
public Fraction(int molecular,int  denominator){

this.molecular = molecular;
this.denominator = denominator;
}

/**
* 加法
* @param 分数a
* @param 分数b
* @return
*/
public  static Fraction fractionAdd( Fraction a,Fraction b){
//通分
int  denominator_ = a.denominator * b.denominator;
int  molecular_  =
a.molecular *(denominator_/a.denominator)
+
b.molecular *(denominator_/b.denominator)
;
int A = molecular_ ;
int B = denominator_;
//求分子分母的最大公因数
while ( A%B != 0 ){
int C = A%B;
A = B;
B = C;
System.out.println(B);
}
//分子和分母都除以最大公因数
return new Fraction(molecular_/B,denominator_/B);
}

public static void main(String[] args) {
Fraction  fraction =Fraction.fractionAdd(new Fraction(1,6), new Fraction(2,5));
System.out.println(fraction.molecular +"__"+fraction.denominator);
}

}

``````

• 打赏
• 举报

``````	public static void main(String[] args) {
System.out.println(new Fraction(1, 3).plus(new Fraction(1, 5)));
}``````
li-heng 2015-10-14
• 打赏
• 举报

baidu_32011679 2015-10-14
• 打赏
• 举报

• 打赏
• 举报

``````class Fraction {
int a;
int b;

Fraction(int a, int b) {
this.a = a;
this.b = b;
}

Fraction plus(Fraction another) {
int c = b * another.b / getGCD(b, another.b);
return new Fraction(a * c / b + another.a * c / another.b, c);
}

private static int getGCD(int a, int b) {
int max = Math.max(a, b);
int min = Math.min(a, b);
return min > 0 ? getGCD(min, max % min) : max;
}

@Override
public String toString() {
return a + " " + b;
}
}``````
yingming_77 2015-10-14
• 打赏
• 举报

50,594

javaspring bootspring cloud 技术论坛（原bbs）

• 近7日
• 近30日
• 至今