zoukankan      html  css  js  c++  java
  • java的分数类

    概述

    分数类在算法中非常重要, 而在java中不那么重要,java基础类库提供 了biginteger了,提供类似方式,

    package 组合数学;
    
    public class Fraction {
    
    	private long Numerator; // 分子
    	private long Denominator; // 分母
    
    	public Fraction(long numerator, long denominator) {
    		this.Numerator = numerator;
    		if (denominator == 0) {
    			throw new ArithmeticException("分母不能为零");
    		} else {
    			this.Denominator = denominator;
    		}
    		change();
    	}
    
    	public Fraction() {
    		this(0, 1);
    	}
    
    	public long getNumerator() {
    		return Numerator;
    	}
    
    	public void setNumerator(long numerator) {
    		Numerator = numerator;
    	}
    
    	public long getDenominator() {
    		return Denominator;
    	}
    
    	public void setDenominator(long denominator) {
    		Denominator = denominator;
    	}
    
    	private Fraction change() {
    		long gcd = this.gcd(this.Numerator, this.Denominator);
    		this.Numerator /= gcd;
    		this.Denominator /= gcd;
    		return this;
    	}
    
    	/**
    	 * 最大公因数
    	 * 
    	 * @param a
    	 * @param b
    	 * @return
    	 */
    	private long gcd(long a, long b) {
    		long mod = a % b;
    		if (mod == 0) {
    			return b;
    		} else {
    			return gcd(b, mod);
    		}
    	}
    
    	/**
    	 * 四则运算
    	 * @return
    	 */
    	public Fraction add(Fraction second) {
    		return new Fraction(this.Numerator * second.Denominator + second.Numerator * this.Denominator,
    				this.Denominator * second.Denominator);
    	}
    
    	public Fraction sub(Fraction second) {
    		return new Fraction(this.Numerator * second.Denominator - second.Numerator * this.Denominator,
    				this.Denominator * second.Denominator);
    	}
    
    	public Fraction multiply(Fraction second) {
    		return new Fraction(this.Numerator*second.Numerator,
    				this.Denominator * second.Denominator);
    	}
    
    	public Fraction devide(Fraction second) {
    		return new Fraction(this.Numerator*second.Denominator,
    				this.Denominator * second.Numerator);
    	}
    
    	@Override
    	public String toString() {
    		return String.format("{%d/%d}", this.Numerator, this.Denominator);
    	}
    	public static void main(String[] args) {
    		Fraction a = new Fraction(7, 32);
    		Fraction b = new Fraction(13, 32);
    		System.out.println(a.add(b));
    	}
    }
    
    
  • 相关阅读:
    erwin逆向工程,logical模型列名修改为中文
    [Leetcode] Two pointer-- 76. Minimum Window Substring
    [Leetcode] Binary search tree -- 173. Binary Search Tree Iterator
    [Leetcode] 684. Redundant Connection
    [Leetcode] Binary tree--653. Two Sum IV
    [Leetcode] Binary tree -- 617. Merge Two Binary Trees
    [Leetcode] Binary tree-- 563. Binary Tree Tilt
    [Leetcode] Binary tree-- 572. Subtree of Another Tree
    [Leetcode] Binary tree-- 437. Path Sum III
    [Leetcode] Binary tree-- 113. Path Sum II
  • 原文地址:https://www.cnblogs.com/dgwblog/p/8099306.html
Copyright © 2011-2022 走看看