java编程题:鸡鸭猪狗4种动物,共30 条腿,要求输出所有可能的动物组合
答案:3 悬赏:10 手机版
解决时间 2021-03-06 08:20
- 提问者网友:我是女神我骄傲
- 2021-03-05 20:58
java编程题:鸡鸭猪狗4种动物,共30 条腿,要求输出所有可能的动物组合
最佳答案
- 五星知识达人网友:零点过十分
- 2021-03-05 21:50
public class Test {
public static void main(String[] args) {
int count = 30;
int possibleNum = 1;
for (int i = 4; i <= count; i += 4) {
for (int j = 4; j <= count - i; j += 4) {
for (int x = 2; x <= count - (j + i); x += 2) {
if (x + j + i == count ) {
System.out.println("*************Possible " + possibleNum++ + ": ");
System.out.println("pig = " + (i/4));
System.out.println("dog = " + (j/4));
System.out.println("chicken = " + (x/2));
}
for (int y = 2; y <= count - (x + j + i); y += 2) {
if (y + x + j + i == count ) {
System.out.println("*************Possible " + possibleNum++ + ": ");
System.out.println("pig = " + (i/4));
System.out.println("dog = " + (j/4));
System.out.println("chicken = " + (x/2));
System.out.println("duck = " + (y/2));
}
}
}
}
}
}
}
public static void main(String[] args) {
int count = 30;
int possibleNum = 1;
for (int i = 4; i <= count; i += 4) {
for (int j = 4; j <= count - i; j += 4) {
for (int x = 2; x <= count - (j + i); x += 2) {
if (x + j + i == count ) {
System.out.println("*************Possible " + possibleNum++ + ": ");
System.out.println("pig = " + (i/4));
System.out.println("dog = " + (j/4));
System.out.println("chicken = " + (x/2));
}
for (int y = 2; y <= count - (x + j + i); y += 2) {
if (y + x + j + i == count ) {
System.out.println("*************Possible " + possibleNum++ + ": ");
System.out.println("pig = " + (i/4));
System.out.println("dog = " + (j/4));
System.out.println("chicken = " + (x/2));
System.out.println("duck = " + (y/2));
}
}
}
}
}
}
}
全部回答
- 1楼网友:山君与见山
- 2021-03-05 23:32
请问鸡鸭可以有几种腿?
- 2楼网友:低血压的长颈鹿
- 2021-03-05 22:12
最简单直接的就是用4个for循环就行了
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯