Java basic------Hundred dollar buy hundred chicken problem

Source: Internet
Author: User

Problem Description: Rooster every 3 Yuan, hen every 5 yuan, chicken three only one yuan, ask 100 yuan to buy 100 chickens have several buy method?

public static void Main (string[] args) {
int count=0; Total scheme number with Count records
Suppose I means buy the number of hens, j means buy the number of cocks, k means buy chickens number
for (int i=0;i<=100/5;i++) {
for (int j=0;j<=100/3;j++) {
for (int k=0;k<=100;k+=3) {//This ensures that the number of chickens is a multiple of 3. The only way to spend money is to be an integer, not a decimal.
if (i+j+k==num && I*5+j*3+k/3==money) {
count++;
System.out.println ("+count +" scheme: Number of hens: "+ i +", Rooster Number: "+ j +", Number of chickens: "+ K);"
}
}
}
}
SYSTEM.OUT.PRINTLN ("Total" + count + "kind of buy Method!") ");
}

Expand here for spending money to buy num Chicken buying Method!!!

public static void Main (string[] args) {
Scanner scan=new Scanner (system.in);
System.out.println ("Please enter the total number of chickens you wish to buy (integers):");
int Num=scan.nextint ();
System.out.println ("Please enter how much you want to pay for these chickens (integers):");
int Money=scan.nextint ();


int count=0; Total scheme number with Count records
Suppose I means buy the number of hens, j means buy the number of cocks, k means buy chickens number
for (int i=0;i<=money/5;i++) {//i is the number of hens, assuming that all money buys a hen, you can buy up to MONEY/5, so I take the value range of 0-MONEY/5
for (int j=0;j<=money/3;j++) {//j represents the number of cocks, assuming that all money buys a rooster, you can buy up to MONEY/3; So the value range of J is 0-MONEY/3
for (int k=0;k<=money*3;k+=3{//k is the number of chicks, assuming that all money buys chickens, you can buy up to money*3, so the value range for K is 0-money*3
if (i+j+k==num && I*5+j*3+k/3==money) {//Note: The number of chicks must be 3 multiples to guarantee that the money spent is an integer, not a decimal condition.
count++;
System.out.println ("+count +" scheme: Number of hens: "+ i +", Rooster Number: "+ j +", Number of chickens: "+ K);"
}
}
}
}
SYSTEM.OUT.PRINTLN ("Total" + count + "kind of buy Method!") ");
}

Attention:

This code snippet (snippet 1):

for (int k=0;k<=money*3; k+=3) {
if (i+j+k==num && i*5+j*3+k/3==money) {  

Equivalent to (Code Snippet 2):

for (int k=0;k<=money*3; k++) {
if (i+j+k==num && k%3==0 && i*5+j*3+k/3==money) {

Code Snippet 1 is more efficient than code snippet 2, so it is recommended that you use code Snippet 1.

Java basic------Hundred dollar buy hundred chicken problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.