每周一题 3n+1问题

3n+1问题

#include<iostream>
#include<math.h>
#include<map>
using namespace std;
map<int, int> visited;
int getlen(int n){
	if(visited[n]==0)
		visited[n]=(n%2==0?1+getlen(n/2):1+getlen(3*n+1));
	return visited[n];
}
int main(){
	int a, b;
	visited[1]=1;
	while(1){
		int maxn=-1;
		if(cin>>a&&cin>>b){
			int m=max(a,b), n=min(a,b);
			for(int i=n; i<=m; i++)
			maxn=max(maxn, getlen(i));
			cout<<a<<" "<<b<<" "<<maxn<<endl;
		}else
			break;	
	}	
	return 0;
} 
posted @ 2019-01-23 21:23  A-Little-Nut  阅读(175)  评论(0编辑  收藏  举报