java 编写 麻烦各位看下

ailuolun 2009-11-23 08:19:21
(鸡兔同笼)从键盘接收两个整数,
分别是整个数量和腿的数量,并判断其合理性,如果合理则打印出鸡兔各多少只。
...全文
172 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT开发者 2012-09-02
  • 打赏
  • 举报
回复
[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那里有运行效果的图片,跟你说的需求一样。
zhongyucai 2009-11-24
  • 打赏
  • 举报
回复
我觉得8楼的已经做对啦,所以我就不多写啦。支持你LZ。努力
wifewifewife 2009-11-24
  • 打赏
  • 举报
回复

total numbers:
30
total legs:
70
rabbit:5,chicken :25
wifewifewife 2009-11-24
  • 打赏
  • 举报
回复

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);
}
}
}
捏造的信仰 2009-11-24
  • 打赏
  • 举报
回复
用行列式解鸡兔同笼问题:
http://blog.csdn.net/YidingHe/archive/2009/11/24/4861445.aspx
京郊-金手指 2009-11-24
  • 打赏
  • 举报
回复
支持5楼的
dudu3052 2009-11-24
  • 打赏
  • 举报
回复
8楼的是对的 怎么5楼的兔子是两只脚的?
dudu3052 2009-11-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 maride 的回复:]
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");
}

不知道是不是这样

[/Quote]
兔子是四只脚的 呵呵
Net_Flasher 2009-11-23
  • 打赏
  • 举报
回复
//我就把输入步骤免了,假设鸡兔共12只,腿数为32

public 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);
}
}
}
}
afunx 2009-11-23
  • 打赏
  • 举报
回复
枚举算法
FOR(兔子个数=0;兔子个数<=输入总个数;兔子个数++)
{鸡的个数=输入总个数-兔子个数
IF 兔子个数*4+鸡的个数*2==输入脚的个数
输出结果;
退出程序;
}
输出不合理
**************************************************
以上是我的算法,你试着编编看,哪里有问题了,再请求大家帮忙,不要期待别人给你现成的代码。
maride 2009-11-23
  • 打赏
  • 举报
回复
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");
}

不知道是不是这样
ailuolun 2009-11-23
  • 打赏
  • 举报
回复
反正谢谢各位啦 大家一起讨论咯
老秋先生 2009-11-23
  • 打赏
  • 举报
回复
不知道是不是你要的哦
老秋先生 2009-11-23
  • 打赏
  • 举报
回复

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+"只鸡脚");
}
}
godismydaughter 2009-11-23
  • 打赏
  • 举报
回复
我也在学java,而且明天要笔试了,

Who can help me?
Mechain 2009-11-23
  • 打赏
  • 举报
回复
我也在努力学习JAVA,支持你快速进步
ailuolun 2009-11-23
  • 打赏
  • 举报
回复
好啊 呵呵 我在学编程啊 就是不知道怎样去写了 所以要大家帮个忙咯
CDSoftwareWj 2009-11-23
  • 打赏
  • 举报
回复
你想要什么?? 我们帮你写代码??? 那以后我们帮你挣钱不?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧