您的位置:首页 > 其它

杭电 1032 The 3n+1问题

2013-02-25 10:08 190 查看
这是杭电上的一道简单题,我很意外我的代码可以AC,我以为这样写会超时!
本来是想建立一个表,通过查表的形式我觉得效率会更高一些,可是在建表的过程中,表中的数据实在难搞(也可能是我

没发现规律),建表问题暂且搁置!

View Code

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

int counter( int n )
{
int cnt = 1;
while( n != 1 )
{
if( !(n%2) )
{
n = n/2;
cnt++;
}
else
{
n = 3*n+1;
cnt++;
}
}
return cnt;
}

int main(int argc, char *argv[])
{
int m, n, i, cnt, max ;
while( scanf( "%d %d", &m, &n ) != EOF )
{
printf( "%d %d ", m, n );
if( m > n )
{
m = m + n;
n = m - n;
m = m - n;
}
max = 0;
for( i = m; i <= n; i++ )
{
cnt = counter(i);
if( max < cnt )
max = cnt;
}
printf( "%d\n", max );
}

system("PAUSE");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: