• 将真分数分解为埃及分数


    描述

    分子为1的分数称为埃及分数。现输入一个真分数(分子比分母小的分数,叫做真分数),请将该分数分解为埃及分数。如:8/11 = 1/2+1/5+1/55+1/110。

    接口说明

     /*
     功能: 将分数分解为埃及分数序列
     输入参数:
         String pcRealFraction:真分数(格式“8/11”)
     返回值:
         String pcEgpytFraction:分解后的埃及分数序列(格式“1/2+1/5+1/55+1/100”)
     */

     public static String  ConvertRealFractToEgpytFract(String pcRealFraction)
     {
      return null;
     }

    知识点 字符串
    运行时间限制 10M
    内存限制 128
    输入

    输入一个真分数,String型

    输出

    输出分解后的string

    样例输入 8/11
    样例输出 1/2+1/5+1/55+1/110
     
    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;
    	}
    }
    

      

  • 相关阅读:
    如何提高软件可维护性
    UML系列 (二)四种关系
    软件工程
    软件工程需求规格说明书
    机房收费系统可行性分析报告
    ThreadStaticAttribute 的使用
    WPF:Border 控件
    几篇介绍在页面中引用脚本文件的技术文档
    收集两篇介绍 Fildder 的文章
    收集三篇关于数据库主键设计的文章
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/5390289.html
Copyright © 2020-2023  润新知