Java programming case 40-comprehensive process control Example 2

Source: Internet
Author: User
Tags integer division

Java programming those things 40-integrated process control Example 2 Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 5.6.1.3 drinking sodaQuestion: there are a total of 1000 bottles of soda. After each drink, an empty bottle is obtained. Each 3 empty bottles can change one bottle of soda. After drinking, an empty bottle is obtained, how many bottles of soda can be consumed in total and how many empty bottles can be used in the end? This problem is actually a typical recurrence problem. Every three empty bottles can be changed to another bottle of new soda. This way, the recurrence continues until the soda cannot be changed. The first idea is to drink one bottle each time, and change a new bottle of soda every three empty bottles until there is no soda in the end. Remember the amount of soda and the number of empty bottles in the program. The implementation code is as follows: int num = 1000; // Number of soda instances int drinknum = 0; // The number of soda drinks int emptynum = 0; // The number of empty bottles while (Num> 0) {// you can drink num if there is soda --; // you can drink a bottle of emptynum ++; // The number of empty bottles increases by 1 drinknum ++; // The number of steam drinks dropped increases by 1 If (emptynum = 3) {// if there are 3 empty bottles, replace num ++; // The number of steam drinks increases by 1 emptynum = 0; // empty bottle quantity cleared} system. out. println ("total number of dropped bottles:" + drinknum); system. out. println ("number of empty bottles remaining:" + emptynum); execute this program and the output result is as follows: total number of empty bottles drinking: 1499 number of remaining empty bottles: 2 in this code, when a bottle of soda is consumed in each cycle, the amount of soda is reduced by 1 and the number of empty bottles is reduced. The number of empty bottles increases by 1, and the total number of empty bottles is increased by 1. Check whether the number of empty bottles reaches 3 each time. If the number reaches 3, change the number of empty bottles to zero. This idea is intuitive, but the number of cycles is large, so the following logic is implemented. The second approach: one-time drinking of all soda, getting all the empty bottles, changing all to soda, then drinking all again, then getting all the empty bottles, and so on, until no soda is available. The implementation code is as follows: int num = 1000; // Number of soda instances int drinknum = 0; // The number of soda drinks int emptynum = 0; // number of empty bottles while (Num> 0) {// drink drinknum + = num if there is soda; // drink all steam drinks emptynum + = num; // The number of empty bottles equals to the remaining number of empty bottles added this time num = emptynum/3; // The number of soft drinks exchanged for emptynum-= num * 3; // The remaining number of empty bottles for this redemption} system. out. println ("total number of dropped bottles:" + drinknum); system. out. println ("number of remaining empty bottles:" + emptynum); in this Code, each time all soda is drunk, that is, num bottles, the total number of empty bottles is increased each time, because there may be less than three empty bottles each time, the total number of empty bottles It is the number of empty bottles added last time and the num bottle that was drunk this time. Next, the dialog soda, the amount of soda that can be exchanged each time is 1/3 of the number of empty bottles. Note that the integer division is used here, the remaining number of empty bottles for this redemption is three times that of the original number of empty bottles minus the amount of soda obtained for the redemption. This is the function completed by a cycle, and so on. 5.6.1.4 daffodilsProblem: The daffodils represent the cubes of each number that are equal to the same number as itself, for example, 370, 3 × 3 × 3 + 7 × 7 + 0 × 0 × 0 = 370, output all the daffodils. This problem shows a basic algorithm-digital splitting. You need to split each number in a number before you can implement this logic. Implementation idea: cycle all three-digit numbers, split the three-digit single-digit, ten-digit, and hundred-digit numbers, and determine whether the three-digit cube is equal to itself. The implementation code is as follows: for (INT I = 100; I <1000; I ++) {// loop all three digits int A = I % 10; // single digit int B = (I/10) % 10; // ten digits int c = I/100; // hundreds of digits // determine the cube and if (A * a * A + B * B * B + C * c = I) {system. out. println (I) ;}} in this code, you can use I and 10 to retrieve the remainder of a single digit. When splitting ten digits, you can use I to divide the number by ten first and remove the single digit, turn the original ten digits into a single digit, and then take the remainder of 10. Because I is a three digit number, divide I by 100 to get a hundred digits, because here all are integer division, there is no problem with decimals. Then you only need to determine whether the cube is equal to itself. Note: Because I is a cyclic variable, the I value cannot be changed here, otherwise it may cause an endless loop.
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.