您的位置:首页 > 其它

UVA 100

2013-04-10 21:21 369 查看


100 - The 3n + 1 problem

Time limit: 3.000 seconds
UVA第一题就有这么多trick , 伤不起啊。

首先 , 输入的两个数不一定谁大谁小 , 但你输出时还要按原顺序 , 

其次 , 中间变量 n 要 long long

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
int main()
{
int st , en , max , sum;
//freopen("in.txt" , "r" , stdin);
while(cin >> st >> en)
{
max = 0;
int a , b;
if(st < en)
{
a = st ;
b = en ;
}
else
{
a = en;
b = st;
}
for(int i = a ; i <= b ; i ++)
{
sum = 1;
long long n = i;
while(n != 1)
{
if(n % 2 == 1)
{
n = 3 * n + 1;

}
else
{
n /= 2;
}
sum ++;
}
if(sum > max)max = sum;
}

printf("%d %d %d\n", st , en , max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 水题