【第七届蓝桥杯】冰雹数

题目:冰雹数

任意给定一个正整数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;
}


C++代码二(实测运行时间 <100毫秒):
//#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;
}

第七届蓝桥杯所有组试题与答案

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Homilier

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值