3n+1问题--递归

版权声明:转载请附上地址 https://blog.csdn.net/weixin_44574520/article/details/87620325

3n+1问题:

Codevs.天梯
在这里插入图片描述
+

Code:

#include <bits/stdc++.h>
using namespace std;

inline int f_(int x,int ans){
	if(x==1) return ans;
	if((x%2)!=0){
		x=3*x+1;
		ans++;
		f_(x,ans);
	}
	else {
		x=x/2;
		ans++;
		f_(x,ans);
	} 
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		scanf("%d",&n);
		printf("%d\n",f_(n,0));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44574520/article/details/87620325