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

将真分数分解为埃及分数

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> input = new ArrayList<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            input.add(in.nextLine());
        }
        char c = '/';
        for (int k = 0; k < input.size(); k++) {
            String[] array = input.get(k).split(c + "");
            int f1 = Integer.parseInt(array[0]);
            int f2 = Integer.parseInt(array[1]);
            //拆成f1个1/f2
            for(int i = 0; i < f1; i++){
                if(i == f1-1){
                    System.out.println("1/" + f2);
                }else{
                    System.out.print("1/" + f2 + "+");
                }
            }
        }

    }

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务