题解 | #百钱买百鸡问题#,刚开始没看懂文言文哈哈

百钱买百鸡问题

http://www.nowcoder.com/practice/74c493f094304ea2bda37d0dc40dc85b

import java.util.ArrayList;
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.hasNextInt()) { // 注意 while 处理多个 case
            int z = 0;
            int x = 0;
            int y = 100 - x - z;
            int nextInt = in.nextInt();
            ArrayList<String> list = new ArrayList<>();
            while (z <= 99){
                while (x <= 100 - z){
                    if ((5 * x) + (3 * y) + (z/3) == 100){
                        list.add(x + " " + y + " " + z);
                    }
                    x += 1;
                    y = 100 - x - z;
                }
                z += 3;
                x = 0;
                y = 100 -x -z;
            }
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
        }
    }
}

全部评论

相关推荐

06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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