Java small algorithm (1)

Source: Internet
Author: User

I. Question 5: Buy a rooster, 3: Buy a hen, and 3: Buy three chicks. How many brokers, hens, and chicks have to buy 100 chickens for one hundred yuan? II. The idea is to first list a mathematical formula. If the rooster, hen, and chicks have their own I, j, and k, then there is an equation: 5i + 3j + k/3 = 100 I + j + k = 100; I, j, k> = 0 obviously, there are multiple solutions to this problem. You can use the enumeration method. The maximum number of cocks is 20, because you need to buy 100. If you want to buy a rooster, the total number cannot meet the requirements. The minimum number is 0. Similarly, the maximum number of hens is 30, there are at least 0 chicks. The situation is special. Although you can buy about 300 chicks, you only need 100 chickens. And it costs 100 yuan, so it is impossible to only sell chicks. 3. Step 1. Create a triple for loop 2. Perform conditional judgment 3. Output 4. Code [java] package datastructure; /*** @ author wangpeng ***/public class Cock_number {/*** @ param args */public static void main (String [] args) {for (int I = 0; I <100/5; I ++) {for (int j = 0; j <100/3; j ++) {for (int k = 0; k <100*3; k ++) {if (I + j + k = 100 & I * 5 + j * 3 + j/3 = 100) {System. out. println ("Rooster:" + I + "\ t hen:" + j + "\ t chicken:" + k) ;}}} package datastructure; /*** @ author wangpeng ***/public class Cock_number {/*** @ param args */public static void main (String [] args) {for (int I = 0; I <100/5; I ++) {for (int j = 0; j <100/3; j ++) {for (int k = 0; k <100*3; k ++) {if (I + j + k = 100 & I * 5 + j * 3 + j/3 = 100) {System. out. println ("Rooster:" + I + "\ t hen:" + j + "\ t chicken:" + k) ;}}}}5. Output Rooster: 0 HEN: 25 chicken: 75 Rooster: 3 HEN: 20 chicken: 77 Rooster: 4 HEN: 18 Rooster: 78 Rooster: 7 HEN: 13 Rooster: 80 Rooster: 8 HEN: 11 chicken: 81 Rooster: 11 HEN: 6 chicken: 83 Rooster: 12 HEN: 4 chicken: 84

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.