@出题人

Brilliance 紫罗兰 2024-07-22 15:02:47 5

这个题目是不是有点难理解

奇数不能一直×3吗

偶数就➗2

(我后面看完题解还没有理解)

{{ vote && vote.total.up }}

共 13 条回复

lyhldy CSP-J2二等
#include<cstdio>
#include<queue>
using namespace std; 
struct find{
	int value,step;
};
int max(int x,int y){
	return x>y? x:y;
}
int bfs(int k){
	queue<find> que;
	int maxn=-1;
	find begin;
	begin.step =1;begin.value =k;
	que.push(begin);
	do{
		find fi=que.front();
		//printf("step=%d value=%d\n",fi.step ,fi.value );
		que.pop();
		maxn=max(fi.step ,maxn);
		if(fi.value >k*k){
			que.pop();
			break;
		}
		if(fi.value %2!=0){
			find work;
			work.step=fi.step +1;
			work.value =fi.value *3;
			que.push(work);
		}
		else{
			find works;
			works.step=fi.step +1;
			works.value =fi.value *3;
			que.push(works);
			works.step=fi.step +1;
			works.value =fi.value/2;
			que.push(works);
		}
	}while(!que.empty());
	return maxn;
}
int a[11451],n,ans=-1,wo;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    	scanf("%d",&a[i]);
    	int wo=bfs(a[i]);
    	ans=max(ans,wo);
	}
	printf("%d\n",ans);
    return 0;
}

我用了广搜,结果写完了发现题目中没说结束条件…

lyhldy CSP-J2二等

确实

Brilliance 紫罗兰