码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 1032 The 3n + 1 problem(水)

时间:2015-02-28 23:04:58      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:hdoj   acm   杭电   

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

【注意】:题目并不是一定按照第一二个数小于等于第二个数输入。如果不是,需要交换。。虽然这种数据感觉很无聊不过在题目中有提示的,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(水)

标签:hdoj   acm   杭电   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!