Write a simple problem of a hundred money and a hundred chickens in JAVA

1. [Write a simple problem of a hundred money and a hundred chickens in JAVA] Analysis
Problem description: One hundred yuan buys one hundred roosters, hens, and chicks.
A rooster is 5 yuan, a hen is 3 yuan, and three chicks are 1 yuan.
One hundred yuan must be spent, and 100 chickens must be bought.
One hundred yuan to buy all roosters, you can buy 20. Therefore, a maximum of 20 roosters
can be bought for 100 yuan for all hens, and 100/3 can be bought. So the hen can buy 100/3 chickens at most
for 100 yuan, you can buy 300 chickens, but you can buy 100 chickens at most, so the maximum is 100.
Define three loops nested
in the first loop the number of roosters the
second loop the number of hens the
third loop the number of chicks The
code is as follows:
//A hundred money and a hundred chickens
public static void main( String[] args ) {

for (int i = 0; i <= 20; i ++) {//Define the initial value of the rooster as 0, add one for each loop, up to 20
for (int j = 0; j <= 100 / 3; j++ ) {//Define the initial value of the hen to be 0, add one for each loop, up to 100/3
for (int k = 0; k <= 100; k++) {//Define the initial value of chicks to 0, add one for each loop, up to 100
if ( i * 5 + j * 3 + k / 3 == 100 && k % 3 == 0 && i + j + k == 100) {//Judging that the total value of the chicken is equal to 100 and the total is also 100 and the chicken The quantity needs to be a multiple of 3, otherwise the money will not be spent
System.out.printf ("Rooster: %2d hen: %2d chick:%2d", i , j, k);//Output the number of rooster hen and chick
System.out.println ();//Newline
}
}
}
}

}

Copyright statement: The content of this article is contributed by Alibaba Cloud real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00