1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n ← 3n + 1
5. else n ← n/2
6. GOTO 2
n == 1
while
讀入兩整數,並用 if
讓 i ≦ j,再把虛擬碼轉成 C 就行,如果有遇到更大值就更新#include<stdio.h>
void compare(int *a, int *b){
int temp;
if(*a > *b){
temp = *a;
*a = *b;
*b = temp;
}
}
int main(){
int i, j;
int n;
while(scanf("%d %d", &i, &j) != EOF){
printf("%d %d ", i, j);
compare(&i, &j); //let i < j
int max = 0;
while(i <= j){
int count = 1;
n = i;
while(n != 1){
if(n % 2 == 1){ //odd
n = 3 * n + 1;
}
else{ //even
n = n / 2;
}
count++; //count the length
}
i++;
if(count > max){
max = count; //update max length
}
}
printf("%d\n", max);
}
return 0;
}