HDU-1032The 3n + 1 problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032

注意:输入不一定保证第一个数就比第二个数小,数据较弱,直接暴力!

#include<cstdio>
#include<iostream>
#define N 1000005
using namespace std;
int fun(int a,int b) {
	int ans=1,len;
	for(int i=a; i<=b; i++) {
		len=1;
		int j=i;
		while(j!=1) {
			if(j%2!=0)
				j=3*j+1;
			else
				j=j/2;
			len++;
		}
		ans=max(ans,len);
	}
	return ans;
}
int main() {
	int a,b;
	while(~scanf("%d%d",&a,&b)) {//输入不一定保证第一个数小于第二个数 
		printf("%d %d %d\n",a,b,fun(min(a,b),max(a,b)));
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39564498/article/details/81705607
今日推荐