1,在⻢克思⼿稿中有⼀道趣味的数学问题:⼀共有30个⼈,可能包括男⼈,⼥⼈和 * ⼩孩。他们在⼀家饭馆吃饭共花了50先令,其中每个男⼈花3先令,每个⼥⼈花2先 * 令,每个⼩孩花1先令。请问男⼈、⼥⼈和⼩孩各⼏⼈?用java语言实现
时间: 2024-06-06 11:11:17 浏览: 131
java 男生女生各多少人
5星 · 资源好评率100%
public class People {
public static void main(String[] args) {
int totalPeople = 30;
int totalMoney = 50;
int manMoney = 3;
int womanMoney = 2;
int childMoney = 1;
int manNum, womanNum;
for(manNum = 0; manNum <= totalPeople; manNum++){
for(womanNum = 0; womanNum <= totalPeople-manNum; womanNum++){
int childNum = totalPeople - manNum - womanNum;
int money = manNum*manMoney + womanNum*womanMoney + childNum*childMoney;
if(money == totalMoney){
System.out.println("男人:"+manNum+"人,女人:"+womanNum+"人,小孩:"+childNum+"人");
}
}
}
}
}
阅读全文