# UVa 100
### 題目連結:[UVa100](http://domen111.github.io/UVa-Easy-Viewer/?100)
### 題述:
考慮以下的演算法:
1.輸入 n
2.印出 n
3.如果 n = 1 結束
4.如果 n 是奇數 那麼 n=3*n+1
5.否則 n=n/2
6.GOTO 2
例如輸入 22 , 得到的數列: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
據推測此演算法對任何整數而言會終止 ( 當列印出 1 的時候 )。雖然此演算法很簡單,但以上的推測是否真實卻無法知道。然而對所有的n ( 0 < n < 1,000,000 ) 來說,以上的推測已經被驗證是正確的。
給一個輸入 n ,透過以上的演算法我們可以得到一個數列 ( 1 作為結尾 ) 。此數列的長度稱為 n 的 cycle-length 。上面提到的例子, 22 的 cycle length 為 16.
---
問題來了:對任2個整數 i , j 我們想要知道介於 i , j ( 包含 i , j ) 之間的數所產生的數列中最大的 cycle length 是多少。
---
輸入可能包含了好幾列測試資料,每一列有一對整數資料 i , j 。 0< i , j < 1,000,000
---
對每一對輸入 i , j 你應該要輸出 i , j 和介於 i , j 之間的數所產生的數列中最大的 cycle length 。
### c code:
```c=
#include<stdio.h>
int tn ( int n ) {
int c = 1 ;
while ( n != 1 ) {
if ( n % 2 == 0 ) {
n /= 2 ;
}
else {
n = n * 3 + 1 ;
}
c++ ;
}
return c ;
}
int main() {
int a = 0 ;
int b = 0 ;
int o , p ;
int tmp = 0 ;
int i = 0 ;
while ( scanf ( "%d%d" , &a , &b ) != EOF ) {
int ans = 0 ;
o = a ;
p = b ;
if ( a > b ) {
tmp = a ;
a = b ;
b = tmp ;
}
for ( i = a ; i <= b ; i++ ) {
if ( tn ( i ) >= ans ) {
ans = tn ( i ) ;
}
}
printf ( "%d %d %d\n" , o , p , ans ) ;
}
}
```

:::success
**``sample input``**1 10
10 1
100 200
201 210
900 1000
:::
:::success
**``sample output``**
1 10 20
10 1 20
100 200 125
201 210 89
900 1000 174
:::
#### [返回首頁](https://hackmd.io/@fkleofk/APCS#100)
###### tags: `APCS選修` `C++` `UVa`