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", &MINN,&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