将真分数分解为埃及分数
| 描述 |
分子为1的分数称为埃及分数。现输入一个真分数(分子比分母小的分数,叫做真分数),请将该分数分解为埃及分数。如:8/11 = 1/2+1/5+1/55+1/110。
接口说明 /* public static String ConvertRealFractToEgpytFract(String pcRealFraction)
|
|---|---|
| 知识点 | 字符串 |
| 运行时间限制 | 10M |
| 内存限制 | 128 |
| 输入 |
输入一个真分数,String型 |
| 输出 |
输出分解后的string |
| 样例输入 | 8/11 |
| 样例输出 | 1/2+1/5+1/55+1/110 |
态度决定高度,细节决定成败,

浙公网安备 33010602011771号
package com.oj5; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { static class FractionNum{ int up; int down; public FractionNum() { } public FractionNum(int up, int down) { this.up = up; this.down = down; } public int getUp() { return up; } public void setUp(int up) { this.up = up; } public int getDown() { return down; } public void setDown(int down) { this.down = down; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); String inputStr = in.nextLine(); FractionNum input = new FractionNum(Integer.parseInt(inputStr.split("\\/")[0]),Integer.parseInt(inputStr.split("\\/")[1])); FractionNum temp = new FractionNum(input.getUp(),input.getDown()); FractionNum num = new FractionNum(); StringBuilder sb = new StringBuilder(); while(isEgpyFraction(temp)){ for(int i = 2;;i++){ int gbs = getGBS(temp.getDown(),i); int bs = gbs/temp.getDown(); int bs2 = gbs/i; int result = bs*temp.getUp()-bs2*1; //System.out.println(result+" "+i); if(result>0){ sb.append(1+"/"+i+"+"); int gys = getGYS(result,gbs); temp.setUp(result/gys); temp.setDown(gbs/gys); //System.out.println(result/gys+"/"+gbs/gys+" "+gys+" "+i); break; } } } sb.append(temp.getUp()+"/"+temp.getDown()); System.out.println(sb.toString()); } private static boolean isEgpyFraction(FractionNum num) { if(num.getUp()==1) return false; return true; } private static int getGYS(int a, int b) { if(b>a){ int temp = b; b = a; a = temp; } while(b!=0){ int c = a%b; a = b; b = c; } return a; } private static int getGBS(int a, int b) { int min,max; if(b>a){ int temp = b; b = a; a = temp; } min = b; max = a; while(b!=0){ int c = a%b; a = b; b = c; } //System.out.println(min*max+" "+a); return (min*max)/a; } private static boolean isEgpyFraction() { // TODO Auto-generated method stub return false; } }