HDOJ 1032 The 3n + 1 problem(水)

本文介绍了一个算法用于计算一组整数中最大循环长度的过程,通过交换条件确保整数顺序不变并输出最大循环长度。

【题意】:获得最大循环长度。

【注意】:题目并不是一定按照第一二个数小于等于第二个数输入。如果不是,需要交换。。虽然这种数据感觉很无聊不过在题目中有提示的,The integers i and j must appear in the outputin the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line). 

【AC代码】:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int getLength(int a)
{
	int cnt = 1;
	while (a!=1)
	{
		cnt++;
		//even
		if (0==a%2)
			a = a/2;
		else
			a = 3*a+1;
	}
	return cnt;
}

int main(int argc, char** argv) {
	
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int a = 0, b = 0;
	while (cin >> a >> b)
	{
		int m = 0, n = 0;
		m = a < b?a:b;
		n = a < b?b:a;
		int i = 0, max = -1;
		for (i = m; i <=n; i++)
		{
			if (max < getLength(i))
				max = getLength(i);
		}
		cout << a << " " << b << " " << max << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值