本站遷移

因為我最近租用了網路空間以及網域,
故本站已遷移至新網站~
這邊的資訊已經正在進行搬移的工作~
希望各位可以到新網站去逛XD

New Website:
http://knightzone.org/

搜尋此網誌

2011年1月19日 星期三

[Zerojudge]c039: The 3n + 1 problem

照著題目所說的去遞迴即可得解。

[C++](34ms, 700KB)
#include<iostream>
using namespace std;
int f( int n )
{
if( n == 1 )
return 1;
else if( n % 2 )
return f( 3*n+1 )+1;
else
return f( n/2 )+1;
}
int main()
{
int i, j;
while( cin >> i >> j )
{
int min_num = min( i, j );
int max_num = max( i, j );
int answer = 0;
for( int n = min_num ; n <= max_num ; n++ )
answer = max( answer, f(n) );
cout << i << ' ' << j << ' ' << answer << endl;
}
return 0;
}

0 意見:

張貼留言