题解 | #将真分数分解为埃及分数#

将真分数分解为埃及分数

https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b

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().split("/");
            long a = Long.parseLong(str[0]);
            long b = Long.parseLong(str[1]);
            long c;
            StringBuilder res = new StringBuilder();

            while (a != 1 && b % a != 0) {
                c = b / a + 1;
                a = a * c - b;
                b = b * c;
                res.append("1/").append(c).append("+");
            }
            if (b % a == 0 && a != 1) {
                res.append("1/").append(b / a);
            } else if (a == 1) {
                res.append("1/").append(b);
            }
            System.out.println(res);
        }
    }
}

全部评论

相关推荐

合适才能收到offe...:招聘上写这些态度傲慢的就别继续招呼了,你会发现hr和面试官挺神的,本来求职艰难就可能影响一些心态了,你去这种公司面试的话,整个心态会炸的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务