Thunder programming questions: programming: Find a number in addition to 2 + 1 In addition to 3 + 2 in addition to 4 + 3 in addition to 5 + 4 in addition to 6 + 5 in addition to 7 + 0

Source: Internet
Author: User
Package com; public class test {public static void main (string [] ARGs) {system. out. println (getsteps1 (); system. out. println (">>>>>>>>>>>>>>>>>>>>>>>>>>>>"); system. out. println (test. getsteps (); system. out. println (">>>>>>>>>>>>>>>>>>>>>>>>>>>> ");} public static int getsteps () {// use the minimum public multiple to reduce the number of traversal times. Int I = 1; int step = 2; Boolean maxstep = false; while (true) {system. Out. Print ("" + I + "); If (! Maxstep) {if (I % 2 = 1) {step = 2;} else {I + = step; continue;} if (I % 3 = 2) {step = 6;} else {I + = step; continue;} if (I % 4 = 3) {step = 12;} else {I + = step; continue;} if (I % 5 = 4) {step = 60; maxstep = true;} else {I + = step; continue ;} if (I % 6 = 5) {step = 60; maxstep = true;} else {I + = step; continue;} if (I % 7 = 0) {return I;} else {I + = step; Continue ;}} else {// Add 60if (I % 7 = 0) {return I ;} else {I + = step;} // else {I + = step; continue;} adds a continue compiler error. If (I % 6 = 5) {} else {I + = step; continue;} if (I % 5 = 4) {} else {I + = step; continue;} if (I % 4 = 3) {} else {I + = step; continue;} if (I % 3 = 2) {} else {I + = step; continue;} if (I % 2 = 1) {} else {I + = step; Continue ;}}}} public static int getsteps1 () {int I = 1; while (true) {if (I % 2 = 1) {} else {I ++; continue ;} if (I % 3 = 2) {} else {I ++; continue;} if (I % 4 = 3) {} else {I ++; continue ;} if (I % 5 = 4) {} else {I ++; continue;} if (I % 6 = 5) {} else {I ++; continue ;} if (I % 7 = 0) {return I;} else {I ++; Continue ;}}}}

The running result is:

119>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1  3  5  11  23  35  47  59  119 119>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

We can see that when I use the minimum public multiple method, we only need to cycle 8 times to find the number of 119. All the later versions will meet the question requirements.

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.