题解 | 除了暴力解法? 还有? #百钱买百鸡问题#

百钱买百鸡问题

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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 a = in.nextInt();
            List<int[]> list = count();
            for(int[] arr:list){
               System.out.println(arr[0]+" "+arr[1]+" "+arr[2]);
            }

        }


    }
    private static List<int[]> count(){
        int x=-1,y=0,z =0;
        List<int[]> list = new ArrayList<>();
        for(int i=0;i<=20;i++){
            y=0;
            x++;
            for(int j=0;j<33;j++){
                z=0;
                y++;
                for(int k=0;k<33;k++){
                    z+=3;
                    if(x+y+z==100 && 5*x+3*y+z/3==100 ){
                        int[] arr = {x,y,z};
                        list.add(arr);
                    }
                }
            }
        }
        return list;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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