随笔 - 7  文章 - 0  评论 - 0  阅读 - 1488

基本算法思想----穷举

1
穷举法求解鸡兔同笼问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//穷举法求解鸡兔同笼问题
import java.util.Scanner;
public class QiongJu {
    static int chicken,habbit;
    public static int qiongJu(int head,int foot){
        int re,i,j;
        re = 0;
        for(i=0;i<=head;i++)
        {
            j=head - i;
            if(i*2 + j*4 == foot)
            {
                re = 1;
                chicken = i;
                habbit = j;
            }
        }
        return re;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int re,head,foot;
        System.out.println("穷举法求解鸡兔同笼问题:");
        System.out.print("请输入头数:");
        Scanner input = new Scanner(System.in);
        head = input.nextInt();
        System.out.print("请输入脚数:");
        foot = input.nextInt();
        re=qiongJu(head,foot);
        if(re == 1){
            System.out.println("鸡有:"+chicken+"只,兔有"+habbit+"只。");
        }
        else{
            System.out.println("无法求解!");
        }
    }

  

posted on   Mathematics  阅读(271)  评论(0编辑  收藏  举报
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示