分子为1的分数称为埃及分数。现输入一个真分数(分子比分母小的分数,叫做真分数),请将该分数分解为不同的埃及分数。如:8/11 = 1/2+1/5+1/55+1/110。
注:真分数指分子小于分母的分数,分子和分母有可能gcd不为1!
如有多个解,请输出任意一个。
输入一个真分数,String型
输出分解后的string
8/11 2/4
1/2+1/5+1/55+1/110 1/3+1/6
第二个样例直接输出1/2也是可以的
import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static class Fraction { public double deno; //分母 public double nume; //分子 public Fraction(double nume, double deno) { this.nume = nume; this.deno = deno; } public Fraction(String FraStr) { this(Double.valueOf(FraStr.split("/")[0]).doubleValue(), Double.valueOf(FraStr.split("/")[1]).doubleValue()); } @Override public String toString() { return String.format("%.0f/%.0f", nume, deno); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case Fraction proper = new Fraction(in.next()); //对这个真分数 //遍历自然数2,3,4,...... //当找到一个自然数x,满足 1/x <= proper时 //proper -= 1/x //将1/x 添加到Egyption //遍历自然数x+1, x+2, x+3, ...... //重复上述步骤直到proper == 0 //其中分数的运算和比较还需要通分和化简,此处省略未表 List<Fraction> retList = new ArrayList<>(); double x = 2.0; while (proper.nume != 0) { while (!isLowerThanPrev(new Fraction(1.0, x), proper)) { x ++; } Fraction frac = new Fraction(1, x); reduction(frac, proper); proper.nume -= frac.nume; simplify(frac); retList.add(frac); if (proper.nume == 1) { retList.add(new Fraction(1, proper.deno)); proper.nume = 0; } } String retStr = String.join("+",retList.stream().map(frac -> frac.toString()).collect(Collectors.toCollection(ArrayList<String>::new))); System.out.println(retStr); } } public static boolean isLowerThanPrev(Fraction current, Fraction proper) { reduction(current, proper); return current.nume <= proper.nume; } public static void reduction(Fraction frac1, Fraction frac2) { //通分 // simplify(frac1); simplify(frac2); double deno1 = frac1.deno; double deno2 = frac2.deno; double deno_ = lcm(Math.max(deno1, deno2), Math.min(deno1, deno2)); //求得通分的分母 frac1.deno = deno_; frac2.deno = deno_; frac1.nume *= deno_ / deno1; frac2.nume *= deno_ / deno2; } public static void simplify(Fraction frac) { //化简 double nume_ = frac.nume; double deno_ = frac.deno; double gcd_ = gcd(deno_, nume_); frac.nume = nume_ / gcd_; frac.deno = deno_ / gcd_; } public static double lcm (double a, double b) { //求最小公倍数 double r = a * b / gcd(a, b); return r;//当a*b的积很大时,有越界风险 } public static double gcd (double m, double n) { //求最大公因数 double t = m % n; while (t != 0) { m = n; n = t; t = m % n; } return n; } }
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] input = in.nextLine().split("/"); for (int i = 0;i < Integer.valueOf(input[0]);i ++){ System.out.print("1/"+input[1]); if (i != Integer.valueOf(input[0])-1){ System.out.print("+"); } } System.out.println(); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String[] frac = in.next().split("/"); long a = Long.parseLong(frac[0]), b = Long.parseLong(frac[1]); for (long c = 0; b % a != 0; a -= b % a, b *= c) { c = b / a + 1; System.out.print("1/" + c + "+"); } System.out.println("1/" + b / a); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String[] str = in.nextLine().split("\\/"); long a = Integer.valueOf(str[0]); long b = Integer.valueOf(str[1]); long c = 0; String res = ""; for ( ; a != 1 && b % a != 0 ;) { c = b / a + 1; a = a * c - b; b = b * c; res += ("1/" + c + "+"); } if (b % a == 0 && a != 1) { res += ("1/" + b / a); } else if (a == 1) { res += ("1/" + b); } System.out.println(res); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String str = in.nextLine(); // 获得输入分子 long a = Integer.parseInt(str.split("/")[0]); // 获得输入分母 long b = Integer.parseInt(str.split("/")[1]); long c = 0; // 定义StringBuffer用于拼接字符串 StringBuffer newStr = new StringBuffer(); /** 数学家斐波那契提出的一种求解埃及数的贪心算法,准确的算法表述应该是这样的: 设某个真分数的分子为a,分母为b; 把c=(b/a+1)作为分解式中第一个埃及数的分母; 将a-b%a作为新的a; 将b*c作为新的b; 如果a等于1,则最后一个埃及数为1/b,算法结束; 如果a大于1但是a能整除b,则最后一个埃及数为1/(b/a),算法结束; 否则重复上面的步骤。 */ while (true) { c = b / a + 1; a = a - b % a; b = b * c; newStr.append("1/" + String.valueOf(c) + "+"); if (a == 1) { newStr.append("1/" + String.valueOf(b)); break; } if (a > 1 && b % a == 0) { newStr.append("1/" + String.valueOf(b / a)); break; } } // 输出结果 System.out.println(newStr.toString()); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNextLine()) { String s = in.nextLine(); String[] parts=s.split("/"); long a=Integer.parseInt(parts[0]); long b=Integer.parseInt(parts[1]); String result=""; long c=0; while(b%a!=0){ c=b/a+1; result=result+"1/"+c+"+"; a=a*c-b; b=b*c; } result=result+"1/"+b/a; System.out.println(result); } } }
import java.util.Scanner; import java.util.List; import java.util.ArrayList; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case List<Integer> list = getEgyptDeno(in.nextLine()); int i = 0, len = list.size(); StringBuffer sb = new StringBuffer(); for (i = 0; i < len; i++) { sb.append("1/").append(list.get(i)); if (i < len - 1) { sb.append("+"); } } System.out.println(sb.toString()); } } public static List<Integer> getEgyptDeno(String str) { String[] arr = str.split("/"); int a = Integer.parseInt(arr[0]), b = Integer.parseInt(arr[1]), c = 0; List<Integer> resList = new ArrayList<>(); while (a != 1 && b % a != 0) { if (b % (a - 1) == 0) { resList.add(b / (a - 1)); a = 1; } else { c = b / a + 1; resList.add(c); a = a * c - b; b = b * c; } } if (a == 1) { resList.add(b); } else if (b % a == 0) { resList.add(b / a); } return resList; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String[] s = in.next().split("/"); int fz = Integer.parseInt(s[0]); StringBuilder sb = new StringBuilder(); while(fz-->0) sb.append("1/"+s[1]+"+"); System.out.println(sb.deleteCharAt(sb.length()-1).toString()); } } }
//这都行 import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String next = sc.next(); String[] split = next.split("/"); String time = split[0]; String m = split[1]; ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < Integer.valueOf(time); i++) { list.add("1/"+m); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.size(); i++) { sb.append(list.get(i)); sb.append("+"); } sb.deleteCharAt(sb.length()-1); System.out.println(sb); } }
//package OnlineTest.normal; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { /*把一个真分数写成多个不相同的分子为1的分数之和*/ /*贪心算法:每次找最大的分子为1的数 * a/b=a/(ak+c)=1/(k+c/a),令a>c,即可得到a/b>1/(1+k); * */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] nums = br.readLine().split("\\/"); int[] num = new int[nums.length]; int top, bottom,k,c; top = num[0] = Integer.valueOf(nums[0]); bottom = num[1] = Integer.valueOf(nums[1]); double sum = 0; k = 0; while (top!=1) { k = (int) bottom / top; c = bottom % top; int rr = 1 + k; System.out.printf("1/%d+", rr); sum += (double) 1 / (1 + k); int[] r = coM(top, bottom, 1, (1 + k)); top = r[0]; bottom = r[1]; } System.out.println(top+"/"+bottom); } public static int[] coM(int t1, int b1, int t2, int b2) { int[] r = new int[2]; int b = b1 * b2; int t = t1 * b2 - t2 * b1; //约分 if (b % t == 0) { b = b / t; t = t / t; } else { for (int i = 2; i < t; i++) { if (t % i == 0 && b % i == 0) { b /= i; t /= i; i=2; } } } r[0] = t; r[1] = b; return r; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String input = sc.next(); String[] arr= input.split("/"); int a = Integer.parseInt(arr[0]); int b = Integer.parseInt(arr[1]); String result = ""; while(true){ if(a==1){ result += "1/"+b; break; } if(a > 1 && b % a == 0){ result += "1/"+(b/a); break; } int c = b / a + 1; result += "1/"+c; a = a - b % a; b = c * b; result += "+"; } System.out.println(result); } } }