2014年6月24日 星期二

uva 100 The 3n + 1 problem

第一次寫 uva ,若有用 // 來寫 comment,需刪掉,因為用 ANSI compiler 時,不認識 //。

注意:1.input 的數字不一定是前小後大。
            2.output印出的前兩個數字為 input 數字輸入的順序。

#include <stdio.h>
#include <stdlib.h>

int stop(int num){
    int counter;

    counter=1;
    while(num!=1){
        ++counter;
        if(num%2==1)
            num=3*num+1;
        else
            num/=2;
    }

    return counter;
}

int main(void){
    int low, high, maximum, temp_l, tmep_h;
    int i, temp;

    while( scanf("%d %d",&low, &high)!=EOF ){
        if(low > high){
            temp_l=high; tmep_h=low;
        }else{
            temp_l=low; tmep_h=high;
        }
        maximum=0;
        for(i=temp_l ; i<=tmep_h ; i++){
            temp = stop(i);
            maximum = temp > maximum ? temp : maximum;
        }
        printf("%d %d %d\n", low, high, maximum);
    }


    return 0;
}

沒有留言:

張貼留言