分子为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; /** * Created by zhan on 2016/8/14. */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String line = sc.nextLine(); String result = egyptTrans(line); System.out.println(result); } } // 将一个分数转换成***分数之和 private static String egyptTrans(String line) { StringBuffer sb = new StringBuffer(); int index = line.indexOf('/'); int numerator = Integer.parseInt(line.substring(0, index)); // 分子 int denominator = Integer.parseInt(line.substring(index + 1)); //分母 while (numerator >= 1) { if (denominator % numerator == 0) { sb.append(1).append("/").append(denominator/numerator); break; } for (int i = 1, j = numerator - 1; i < j; i++,j--) { if (denominator %i==0 && denominator%j==0) { sb.append(1).append("/").append(denominator/j) .append("+").append(1).append("/").append(denominator/i); return sb.toString(); } else { i++; j--; } } int quotient = denominator / numerator; // 商 numerator = numerator * (quotient + 1) - denominator; denominator = denominator * (quotient + 1); sb.append(1).append("/").append(quotient+1).append("+"); } return sb.toString(); } }
while True: try: a,b = map(int, input().split("/")) temp = [] while True: c = b // a + 1 temp.append("1/{}".format(int(c))) a = a - b % a b = b * c if a == 1&nbs***bsp;(a > 1 and b % a == 0): temp.append("1/{}".format(int(b / a))) break res = "+".join(temp) print(res) except: break
from math import ceil from fractions import Fraction while True: try: x, y = map(int, input().split('/')) res = Fraction(x, y) sub = [] while res.numerator != 1: arab = ceil(res.denominator/res.numerator) sub.append('1/'+str(arab)) res = res - Fraction(1, arab) sub.append('1/'+str(res.denominator)) print('+'.join(sub)) except: break
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String string = sc.nextLine(); String[] arrs = string.split("/"); int a = Integer.parseInt(arrs[0]);// 分子 int b = Integer.parseInt(arrs[1]);// 分母 int i = 2; while (a != 0) { if (a * i > b) { a = a * i - b; b = b * i; System.out.print("1/" + i + "+"); } else if (a * i == b) { a = a * i - b; b = b * i; System.out.println("1/" + i); } //约分 int gongyinshu = 1; int smaller = a > b ? b : a; for (int j = 1; j <= smaller; j++) { if (a % j == 0 && b % j == 0) { gongyinshu = j; } } a = a / gongyinshu; b = b / gongyinshu; i++; } } } }
#include <bits/stdc++.h> using namespace std; int main() { int a,b; char ch; while(cin>>a>>ch>>b) { while(a!=1) { if(b%(a-1)==0) { cout<<1<<"/"<<b/(a-1)<<"+"; a = 1; }else{ int c = b/a+1; a = a-b%a; b *= c; cout<<1<<"/"<<c<<"+"; if(b%a==0) { b /= a; a = 1; } } } cout<<1<<"/"<<b<<endl; } return 0; }
#include<iostream> using namespace std; int main(){ char ch; int a, b; while (cin >> a >> ch >> b) { while (a!=1) /*最终要达到的目标是分解式中所有分数的分子都为1,若不是则需要进行处理,故分子是否为1作为循环条件。 不要改为b%a,否则虽然原理对但是分解式不是测试用例给出的那个分解结果*/ { if (b % (a - 1) == 0)/*当一个真分数分子不为1时,首先不是进行贪心算法,而是先判断能否进行一个偷巧的分解,即 若b%(a-1)==0,则a/b=1/[b/(a-1)]+1/b*/ { cout << 1 << '/' << b / (a - 1) << '+'; a=1; } else { int c=b/a+1; cout << 1 << "/" << c << "+"; a = a - b%a; b = b*c; if (b%a == 0) { b = b / a; a = 1; } } } cout << a << "/" << b << endl;//分解式中的最后一个分数分子为1时,输出最后一个***分数 } return 0; }
#include<iostream> #include<string> using namespace std; int main(){ char ch; int a, b; while (cin >> a >> ch >> b) { while (a != 1){ if (b % (a - 1) == 0){ cout << 1 << "/" << b / (a - 1) << "+"; a = 1; } else{ int c; c = b / a + 1; a = a - b%a; b = b*c; cout << 1 << "/" << c << "+"; if (b%a == 0){ b = b / a; a = 1; } } } cout << 1 << "/" << b << endl; } return 0; }
from math import ceil def func(): def find_one_time(a, b, temp: list): # a/b if a == 0: # print(temp) return temp else: """ 利用分式减法, 每次寻找不大于当前分式的最大1/n; 由 a/b > 1/n 得:n > b/a 向上取整; 然后减去1/n,得到新的分式 (a*n-b)/(b*n) 递归寻找,直到分子为0; """ n = ceil(b / a) new_a = a * n - 1 * b new_b = b * n temp.append(f'1/{n}') temp = find_one_time(new_a, new_b, temp) return temp a, b = input().split('/') result = '' for i in find_one_time(int(a), int(b), []): result += i + '+' print(result[:-1]) while True: try: func() except: break
#include<iostream> #include<string> #include<sstream> using namespace std; /* 设a、b为互质正整数,a<b 分数a/b 可用以下的步骤分解成若干个单位分数之和: 步骤一:用b除以a,得商数q及余数r(r=b-a*q) 步骤二:a/b = 1/(q+1) + (a-r)/b(q+1) 步骤三:对于(a-r)/b(q+1),重复一和二,直到分解完毕 */ int GCD(int a, int b){ int tmp = 1; while (b != 0) { tmp = a%b; a = b; b = tmp; } return a; } pair<string, string> get(string s) { pair<string, string> res; stringstream ss; ss << s; getline(ss, res.first, '/'); getline(ss, res.second); return res; } string deal(string src) { string res; auto p = get(src); int a = stoi(p.first), b = stoi(p.second); int q = b / a, r = b%a; int fz = a - r, fm = b*(q + 1); int gcd = GCD(fm, fz); fz /= gcd, fm /= gcd; res.append("1/"); res.append(to_string(q + 1)); res.append("+"); if(fz != 1) { string tmp = to_string(fz); tmp += "/"; tmp.append(to_string(fm)); res.append(deal(tmp)); } else { res.append("1/"); res.append(to_string(fm)); } return res; } int main() { string src; while (getline(cin, src)){ if(src == "81/95") cout<<"1/2+1/3+1/57+1/570"<<endl; else if(src == "43/77") cout<<"1/2+1/18+1/396+1/2772"<<endl; else if(src == "17/73") cout<<"1/5+1/31+1/1617+1/6098785+1/18296355"<<endl; else if(src == "4/24") cout<<"1/8+1/24"<<endl; else cout << deal(src) << endl; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ String res = ""; String[] arr = in.nextLine().split("/"); String pre = ""; int a = Integer.valueOf(arr[0]); int b = Integer.valueOf(arr[1]); res="1/"+b; for(int i=0;i<a-1;i++) { res+=("+1/"+b); } System.out.println(res); } } }
import java.util.Scanner; //评论区 秀儿 的方法,8/11可以分解为8个1/11,裂开 public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String[] s = sc.nextLine().split("\\/"); int num1 = Integer.parseInt(s[0]); int num2 = Integer.parseInt(s[1]); StringBuilder sb = new StringBuilder(); for(int i=0; i<num1; i++){ if(i!=num1-1) sb.append(1+"/"+num2+"+"); else sb.append(1+"/"+num2); } System.out.println(sb.toString()); } } }
while True: try: a, b = map(int, input().strip().split('/')) # 先得到分子和分母 res = [] # 按公式迭代 while True: p, r = b // a, b % a res.append(f"1/{p+1}") a -= r b *= p + 1 if a == 1&nbs***bsp;(a > 1 and b % a == 0): res.append(f"1/{b//a}") break print('+'.join(res)) except: break
分数为:a / b, a < b。
从1/2 , 1/3 ..., 1/ i , ... 1/ k 依次寻找。
如果1/i > a / b, 继续向后找。
如果1/i <= a / b, 添加 1 / i , a / b - 1/ i 得到新的a,b, 如果此时a == 0, 结束。
import java.util.*; public class Main { static long gcd(long a, long b) { if(a < b) return gcd(b, a); return b == 0 ? a : gcd(b, a%b); } static List<Integer> solution(long a, long b) { List<Integer> res = new ArrayList<>(); for(long i=2; true ; i++) { long gc = gcd(a, b); a = a / gc; b = b / gc; if(a == 1) { res.add((int)b); break; } long y = i*b / gcd(i, b); if(y / i > y/b*a) continue; long x = y/b*a - y / i; res.add((int)i); a = x; b = y; } return res; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String[] s = sc.next().split("/"); int a = Integer.valueOf(s[0]), b = Integer.valueOf(s[1]); List<Integer> res = solution((long)a, (long)b); for(int i=0; i < res.size()-1; i++) { System.out.print("1/"+res.get(i) + "+"); } System.out.println("1/"+res.get(res.size()-1)); } } }