UVA 3*n+1 problem

Source: Internet
Author: User

UVA 3*n+1 problem.

Solution: M,n (M<n&&0<m,n<1 000 000) for the given boundary, and the maximum number of steps for X (m-1<x<n+1) to 1.

The maximum number of steps is obtained by calculating the M to n sequentially.

Note: 1. Enter the format.

Use while (scanf ("%d%d", &m,&n)!=eof)

{

}

2. The output is the same as the problem.

3. The selection of the variable type when the boundary is 1<n<1 000 000.

The code is as follows:

#include <stdio.h>#include<stdlib.h>voidFindresult (intMin,intMax) {    inti,count,temp; Temp=0;//maximum number of steps for temp storage    Long LongN//Note that the N type is a long long, and when this is int, the 837799 overflow is calculated     for(i=min;i<=max;i++) {n=i; Count=0;  while(1)        {            if(n==1) {Count++; if(temp<count) {Temp=count; }                 Break; }            if(n%2==1) {n=3*n+1; Count++; }            Else{n=n/2; Count++; }}} printf ("%d\n", temp);}intMain () {intMaxn,minn,t;//Minn The lower bound, MAXN is the upper bound. t=0;  while(SCANF ("%d%d", &AMP;MINN,&AMP;MAXN)! =EOF) {printf ("%d%d", MINN,MAXN); if(minn>MAXN) {T=Minn; Minn=MAXN; MAXN=T;   } findresult (MINN,MAXN); //calculate the maximum number of steps    }    return 0;}

Please note that the above code is insufficient, thank you

If there is a better way, hope to enlighten, we learn from each other progress.

UVA 3*n+1 problem

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.