UVa 371 - Ackermann Functions

来源:互联网 发布:中国第一美女程序员 编辑:程序博客网 时间:2024/05/16 05:45

题目:3n+1经典问题,求区间中最大运算次数。

分析:模拟。直接在区间中计算,取最值即可。

注意:1.区间的端点可能是反的;2.计算过程中用long long防止溢出。

#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;int X( long long n ){if ( n == 1LL ) return 3;int count = 0;while ( n > 1LL ) {if ( n&1 ) n = (n<<1LL)+n+1LL;else n >>= 1LL;count ++;}return count;}int main(){int L,H,V,S;while ( scanf("%d%d",&L,&H) && L+H ) {if ( L > H ) swap( L, H );V = L;for ( int i = L+1 ; i <= H ; ++ i )if ( X( V+0LL ) < X( i+0LL ) )V = i;printf("Between %d and %d, %d generates the ",L,H,V);printf("longest sequence of %d values.\n",X(V+0LL));}return 0;}

0 0
原创粉丝点击