题目:冰雹数
任意给定一个正整数N,
如果是偶数,执行: N / 2
如果是奇数,执行: N * 3 + 1
生成的新的数字再执行同样的动作,循环往复。
通过观察发现,这个数字会一会儿上升到很高,
一会儿又降落下来。
就这样起起落落的,但最终必会落到“1”
这有点像小冰雹粒子在冰雹云中翻滚增长的样子。
比如N=9
9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
可以看到,N=9的时候,这个“小冰雹”最高冲到了52这个高度。
输入格式:
一个正整数N(N<1000000)
输出格式:
一个正整数,表示不大于N的数字,经过冰雹数变换过程中,最高冲到了多少。
例如,输入:
10
程序应该输出:
52
再例如,输入:
100
程序应该输出:
9232
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
C++代码三(实测运行时间 <550毫秒):
C++代码四(实测运行时间 <950毫秒):
第七届蓝桥杯所有组试题与答案
任意给定一个正整数N,
如果是偶数,执行: N / 2
如果是奇数,执行: N * 3 + 1
生成的新的数字再执行同样的动作,循环往复。
通过观察发现,这个数字会一会儿上升到很高,
一会儿又降落下来。
就这样起起落落的,但最终必会落到“1”
这有点像小冰雹粒子在冰雹云中翻滚增长的样子。
比如N=9
9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
可以看到,N=9的时候,这个“小冰雹”最高冲到了52这个高度。
输入格式:
一个正整数N(N<1000000)
输出格式:
一个正整数,表示不大于N的数字,经过冰雹数变换过程中,最高冲到了多少。
例如,输入:
10
程序应该输出:
52
再例如,输入:
100
程序应该输出:
9232
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
C++代码一(实测运行时间 <850毫秒) :
//#include <ctime>
#include <iostream>
using namespace std;
long long getMax(long long n)
{
long long max = n;
while ( n != 1)
{
n = n % 2 ? 3 * n + 1 : n / 2;
if (n > max) max = n;
}
return max;
}
int main()
{
long long N, i, t, max = 0;
cin >> N;
for (i = 1; i <= N; ++i)
{
t = getMax(i);
if (t > max)
max = t;
}
cout << max << endl;
// cout << clock() << "ms" << endl;
return 0;
}
//#include <ctime>
#include <iostream>
using namespace std;
long long getMax(long long n)
{
long long max = n;
while ( n != 1)
{
n = n % 2 ? 3 * n + 1 : n / 2;
if (n > max) max = n;
}
return max;
}
int main()
{
int N, max, t, i;
cin >> N;
if (N < 113383)
{
max = getMax(N);
for (i = N - 1; i >= 1; --i)
{
t = getMax(i);
if ( t > max )
max = t;
}
cout << max << endl;
} else if (N < 138367)
cout << 2482111348LL << endl; // 必须带LL或ULL后缀
else if (N < 159487)
cout << 2798323360LL << endl;
else if (N < 270271)
cout << 17202377752LL << endl;
else if (N < 665215)
cout << 24648077896LL << endl;
else if (N < 704511)
cout << 52483285312LL << endl;
else
cout << 56991483520LL << endl;
// cout << clock() << "ms" << endl;
return 0;
}
C++代码三(实测运行时间 <550毫秒):
//#include <ctime>
#include <iostream>
using namespace std;
long long getMax(long long n)
{
long long max = n;
while ( n != 1)
{
n = n % 2 ? 3 * n + 1 : n / 2;
if (n > max) max = n;
}
return max;
}
int main()
{
long long N, i, s, t, max = 0;
cin >> N;
s = N < 2000 ? 1 : N * 2 / 5;
for (i = s; i <= N; ++i)
{
t = getMax(i);
if (t > max)
max = t;
}
cout << max << endl;
// cout << clock() << "ms" << endl;
return 0;
}
C++代码四(实测运行时间 <950毫秒):
//#include <ctime>
#include <iostream>
using namespace std;
int main()
{
long long N, n, k, max = 1;
cin >> N;
for (k = 1; k <= N; ++k)
{
n = k;
while ( n != 1)
{
n = n % 2 ? 3 * n + 1 : n / 2;
if (n > max) max = n;
}
}
cout << max << endl;
// cout << clock() << "ms" << endl;
return 0;
}
C++代码五(实测运行时间 <250毫秒):
//#include <ctime>
#include <iostream>
using namespace std;
long long getMax(long long n)
{
long long max = n;
while ( n != 1)
{
n = n % 2 ? 3 * n + 1 : n / 2;
if (n > max) max = n;
}
return max;
}
int main()
{
long long N, i, t, max = 0;
cin >> N;
if (N > 2) {
i = N / 2;
if (i % 2 == 0) ++i; // i的起始值至少是N/2
for ( ; i <= N; i += 2 ) // 只需对奇数求最大值
{
t = getMax(i);
if (t > max)
max = t;
}
} else
max = N;
cout << max << endl;
// cout << clock() << "ms" << endl;
return 0;
}
第七届蓝桥杯所有组试题与答案