一个蒟蒻的问题
  • 板块灌水区
  • 楼主Aric111
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/8/7 18:43
  • 上次更新2024/8/7 19:57:21
查看原帖
一个蒟蒻的问题
1411161
Aric111楼主2024/8/7 18:43

下面的代码是求一个数的最大质因数的,TLE了,质数函数也优化过了,实在不知道该咋优化了(知道哪里,就是那个主函数里的for循环,但是太蒻了)(0<=n<=2*1e9)

#include<bits/stdc++.h>
using namespace std;
int maxn;
int zs(int n){
	for(int i=2;i*i<=n;i++){
		if(n%i==0){
			return 0;
		}
	}
	return 1;
}		
int main(){
	int n;
	cin>>n;
	if(zs(n)){
		cout<<n;
		return 0;
	}
	for(int i=2;i<=n;i++){
		if(n%i==0&&zs(i)){
			maxn=max(maxn,i);
		}
	}
	cout<<maxn;
	return 0;
}
2024/8/7 18:43
加载中...