3N + 1 data chain

Source: Internet
Author: User

The problem is as follows:
1. Enter a positive integer n;
2. display n;
3. If n = 1, all operations are terminated;
4. If n is an odd number, n is changed to 3 * n + 1; otherwise, n is changed to n/2;
5. Transfer in step 2.

Problem Analysis:
This question is relatively simple. We only need to follow the requirements of the question step by step to find the result.


# Include <iostream> using namespace STD; int Max, M; void Link (int n) {cout <n <''; m ++; if (n = 1) {cout <Endl; return;} If (N % 2 = 1) n = 3 * n + 1; Elsen = n/2; link (n) ;}int main () {int I, j; CIN >>> J; max = 0; For (int t = I; t <= J; t ++) {m = 0; cout <"when T is" <t <", its sequence is:"; Link (t ); cout <"when the positive integer value is" <t <", its chain length is" <m <Endl; If (M> MAX) max = m;} cout <"its longest chain length is" <max <Endl; return 0 ;}

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.