U E D R , A S I H C RSS

3n+1 Problem/김태진

Status

Memory164KTime0MS
개발시간: 1시간, 입출력문제->1시간


Source Code

#include <stdio.h>

int main()
{
    int i,j,max=0,n=1,k,b,c;
    unsigned int a=0;
    int A[10000]={0};
    
    while(1){
        scanf("%d",&b);
        scanf("%d",&c);
        if(b>c) {j=c; k=b;}
        else {j=b; k=c;}
    //  조건?참일경우:거짓일경우;
        for(i=j;i<=k;i++){
            a=i;
            while(1){
                if(a==1) break;
                if(a<i&&a>j) {n=A[i]; break;} 
                if(a%2==1) {a=(3*a+1)>>1; n++;}
                else a=a>>1;
                n++;
            }
            A[i]=n; //DP만드는중
            if(max<n) max=n;
            n=1;
        
        }
        if(feof(stdin)) break;
        
        printf("%d %d %d\n",b,c,max);
        max=0;
    }
    return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:17
Processing time 0.0092 sec