用JAVA,编出:公鸡3元1只 ,母鸡5元一只 ,小鸡3只1元,用100元共有多少种方式能组成100只鸡
来源:百度知道 编辑:UC知道 时间:2024/08/22 19:53:52
很简单的穷举法,希望对你有帮助
public class chicken {
public static void main(String[] args) {
int a;
int b;
int c;
for(a = 0; a < 100;a++){
for(b = 0;b < 100 - a; b++){
c = 100 - a -b;
if(a + b + c == 100 && 1 * a + 5 * b + 3 * c == 100 ){
System.out.println("xiaoji =" + a);
System.out.println("muji =" + b);
System.out.println("gongji =" + c);
System.out.println();
}
}
}
}
}
两个简单的循环就可以啦!
public class OutputTheNumberOfChicken {
public static void main(String[] args) {
int numCock=0, numHen=0,numChick=0;
System.out.println("Cock Hen Chick");
for(numCock=1;numCock<20;numCock++)
{
for(numChick=3;numChick<100;numChick=numChick+3)
{
numHen=100-numCock-numChick;<