(鸡兔同笼)从键盘接收两个整数,
分别是整个数量和腿的数量,并判断其合理性,如果合理则打印出鸡兔各多少只。

解决方案 »

  1.   

    我也在努力学习JAVA,支持你快速进步
      

  2.   

    我也在学java,而且明天要笔试了,
      
    Who can help me?
      

  3.   


    import java.util;
    public class H{
     public static void main(String[] args){
       int tu;
       int ji;
      Scanner name = new Scanner(System.in);
        System.out.print("输入兔有几只脚:");
        tu = name.nextInt();
        System.out.print("输入鸡有几只脚:);
        ji = name.nextInt();
       if(tu%2==0||ji%2==0){
          System.out.println("一共有"+tu/2+"只兔"+ji/2+"只鸡");
       }else{
          int tuSum = tu % 2;
          int jiSum = ji % 2;
          System.out.println("多出了"+tuSum+"只兔脚"+jiSum+"只鸡脚");
    }  
    }
      

  4.   

    import java.util; 
    public class H{ 
    public static void main(String[] args){ 
      int count; 
      int num; 
      int flag=0;// 标记
      Scanner name = new Scanner(System.in); 
        System.out.print("输入兔和鸡共几只:"); 
        count = name.nextInt(); 
        System.out.print("输入兔和鸡共有几只脚:); 
        num = name.nextInt();
    for(int i=1;i< count;i++){
      if((i*2+(count-i)*4)==num)flag=1; 
    }
    if(flag==1)
          System.out.println("一共有"+count-i+"只兔"+i+"只鸡"); 
     else
                System.out.println("ERROR"); 
    }不知道是不是这样
      

  5.   

    枚举算法
    FOR(兔子个数=0;兔子个数<=输入总个数;兔子个数++)
    {鸡的个数=输入总个数-兔子个数
    IF 兔子个数*4+鸡的个数*2==输入脚的个数
        输出结果;
        退出程序;
    }
    输出不合理
    **************************************************
    以上是我的算法,你试着编编看,哪里有问题了,再请求大家帮忙,不要期待别人给你现成的代码。
      

  6.   

    //我就把输入步骤免了,假设鸡兔共12只,腿数为32public class Test
    {
    public static void main(String[] args)
    {
    int num1=12;
    int num2=36;
    for(int i=1;i<12;i++)
    {
    if((i*2+(num1-i)*4)==32)
    {
    System.out.println("The chenken number is :"+i);
    }
    }
    }
    }
      

  7.   

    用行列式解鸡兔同笼问题:
    http://blog.csdn.net/YidingHe/archive/2009/11/24/4861445.aspx
      

  8.   


    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("total numbers:");
    int total = sc.nextInt();
    System.out.println("total legs:");
    int legs = sc.nextInt();
    for (int chicken = 0; chicken < total; chicken++) {
    if (total - chicken == (legs - chicken * 2) / 4 && (legs - chicken * 2) % 4 == 0) {
    System.out.println("rabbit:" + (total - chicken) + ",chicken :" + chicken);
    }
    }
    }
      

  9.   


    total numbers:
    30
    total legs:
    70
    rabbit:5,chicken :25
      

  10.   

    [java]代码
    01 import java.util.*;  02    03 /**  04  * 鸡兔同笼问题  05  */ 06 public class JiTu {  07     public static void main(String args[]) {  08         int head = 0;  09         int foot = 0;  10         String in = "";  11         String[] parameters;  12         Scanner input = new Scanner(System.in);  13         System.out.println("请输入头和脚的数量(用英文逗号隔开):");  14    15         try {  16             while (true) {  17                 in = input.next();  18                 parameters = in.split(",");  19                 head = Integer.parseInt(parameters[0]);  20                 foot = Integer.parseInt(parameters[1]);  21    22                 if (foot < 2 || foot % 2 != 0) {  23                     System.out.println("请输入正确的脚数...");  24                     continue;  25                 } else {  26                     break;  27                 }  28             }  29         } catch (Exception e) {  30             // TODO Auto-generated catch block  31             System.out.println("输入有误!程序退出。");  32             System.exit(-1);  33         }  34    35         int ji = 0, tuMax;  36         tuMax = foot / 4; // 兔子的最大值  37         for (int tu = 0; tu <= tuMax; tu++) {  38             ji = (foot - 4 * tu) / 2; // 从兔子个数为0开始测试,穷举所有情况  39             if (ji + tu == head) { // 如果鸡兔数量=头数,则输出  40                 System.out.println("鸡有" + ji + "只\t兔子有" + tu + "只");  41             }  42    43         }  44     }  45 } 
    源码:http://yuncode.net/code/c_5040baf1416e313那里有运行效果的图片,跟你说的需求一样。