将真分数分解为埃及分数
2016-04-14 11:07
369 查看
描述 | 分子为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; } }
相关文章推荐
- OC学习日记Day3:数据在内存的储存形式,CPU查找内存数据
- 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件
- 深入分析Java的序列化与反序列化
- C语言中char int转换问题
- Android AndFix热补丁动态修复框架使用教程
- java并发编程使用锁进行数据同步操作一
- ROS_Navigation专题1 - Introduction and Learning
- 【SSM-SpringMVC框架】注解的处理器适配器和映射器
- 安卓联系人源码 之AsyncTaskExecutors分析
- Show me the code之Python练习册 Q13 获取网络图片
- 佛祖保佑,永无bug
- php redis
- MOTT的学习(一)
- 保存图片时,设置压缩质量
- 微信开发学习 问题1: 网页授权问题 “该连接无法访问” 解决方法
- 使用chrome的xpath工具
- 【openstack】【nova】【libvirt】在计算节点上实现的imagecache
- 推送对账文件的3种
- 题
- Show me the code之Python练习册 Q11~12 关键词过滤