首页 > 其他 > 详细

HDOJ 1032 The 3n + 1 problem(水)

时间:2015-02-28 23:03:27      阅读:291      评论:0      收藏:0      [点我收藏+]

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

【注意】:题目并不是一定按照第一二个数小于等于第二个数输入。如果不是,需要交换。。虽然这种数据感觉很无聊不过在题目中有提示的,The integers i and j must appear in the output in 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;
}


HDOJ 1032 The 3n + 1 problem(水)

原文:http://blog.csdn.net/weijj6608/article/details/43991555

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!