已知籠中有頭h個,有腳f條,問籠中雞兔各有多少隻

    描述
    已知籠中有頭h個,有腳f條,問籠中雞兔各有多少隻, 如果無法組成
    輸入
    h(0<h<2147483647)
    f(0<f<2147483647)
    輸出
    雞的數目
    兔子的數目

public class Main {
    public static void main(String[] args){
        int h = 0;
        int f = 0;
        Scanner scanner = new Scanner(System.in);
        h = scanner.nextInt();
        f = scanner.nextInt();
        scanner.close();
        int j = 0;    //雞的數量
        int t = 0;    //兔的數量
        for (j = 1; j <= h; j++) {
            for (t = 1; t <= h; t++) {
                if(j+t==h && j*2+t*4 == f){
                    System.out.println(j);
                    System.out.println(t);
                }
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章