一个问题
查看原帖
一个问题
1125478
PeppaYang楼主2025/2/4 18:07
#include<bits/stdc++.h>
using namespace std;
long long n,x,m,ans,cnt;
bool p;
bool is(long long c){
	if(c<2) return 0;
	for(int i=2;i<=sqrt(c);i++){
		if(c%i==0){
			return 0;
		}
	}
	return 1;
}

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		ans=0;
		cnt=1;
		if(x==1){
			cout<<"1\n";
		}
		if(x==0){
			cout<<"0\n";
		}
		while(x){
			if(is(x)==1){
				ans++;
				break;
			}
			x-=cnt;
			if(x<0){
			    break;
			}
			cnt<<=1;
			ans++;
		}
		if(x<0){  
			cout<<"-1\n";
			continue;
		}
		cout<<ans<<endl;
	}
	return 0;
} 

这是90分代码

#include<bits/stdc++.h>
using namespace std;
long long n,x,m,ans,cnt;
bool p;
bool is(long long c){
	if(c<2) return 0;
	for(int i=2;i<=sqrt(c);i++){
		if(c%i==0){
			return 0;
		}
	}
	return 1;
}

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		ans=0;
		cnt=1; //删掉特判
		while(x){
			if(is(x)==1){
				ans++;
				break;
			}
			x-=cnt;
			if(x<0){
			    break;
			}
			cnt<<=1;
			ans++;
		}
		if(x<0){  
			cout<<"-1\n";
			continue;
		}
		cout<<ans<<endl;
	}
	return 0;
} 

这是AC代码
特判错了吗?

2025/2/4 18:07
加载中...