Challenge programming Programming Competition Training Manual-1.6.1 3N + 1 Questions (3N + 1 problem)

Source: Internet
Author: User

Challenge programming Programming Competition Training Manual-1.6.13N + 1 problem(3N + 1 problem)

Code:
Public class problem_3n_add_1 {/*** @ Param ARGs */public static void main (string [] ARGs) {int I = 900; // The initial value is Int J = 1000; // final value int result = maxcount (I, j); // maximum number of operations system. out. print (I + "" + J + "" + result); // output result} Private Static int maxcount (int I, Int J) {// the minimum number of cycles to the maximum number of operations to obtain the maximum number of operations int result; // declare the variable and save the calculation result int tempcount = 0; // define a temporary counter, record the number of computations of a number for (INT x = I; x <= J; X ++) {// compare each integer from I to J, use the 3N + 1 algorithm to obtain the maximum cyclic section. Result = x; // initialize the calculation result variable. The initial value is int COUNT = 1, which is returned by a loop in the smallest I-maximum J. // temporary data, record the number of computations required to complete the 3N + 1 algorithm do {// 3N + 1 algorithm result = judge (result); // This result is taken to count ++ for the next computation; // record count + 1} while (result! = 1); If (tempcount <count) // returns the maximum value tempcount = count;} return tempcount;} Private Static int judge (Int J) {// judge the parity int result = 0; If (J % 2! = 0) {result = J * 3 + 1; // odd number * 3 + 1} else {result = J/2; // even number/2} return result ;}}

Result:

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.