10 条题解

  • 1
    @ 2024-12-29 9:13:56

    理论上讲,由于c++的特性
    int直接舍弃小数位

    我们分类讨论:

    如果n为平方数

    那么sqrt(n)*sqrt(n)=n
    刚好可以判断到

    如果n不是平方数

    那么sqrt(n)*sqrt(n)<n
    okk

    什么?你问我为啥不是枚举到n/2?

    随便拆一个数举例子:
    12
    =1 x 12
    =2 x 6
    =3 x 4
    可以发现,左右两个数列都在逼近sqrt(12)
    而c++特性告诉我们:int(sqrt(12))=3
    所以说直接从2枚举到sqrt(n)就可以了
    1肯定不用枚举
    你家哪个整数不能被1整除啊......

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    	int n;
    	cin>>n;
    	for(int i=2;i<=sqrt(n);i++){
    		if(n%i==0){
    			cout<<"no";
    			return 0;
    		}
    	}
    	cout<<"yes";
    	return 0;
    }
    

    信息

    ID
    3
    时间
    5000ms
    内存
    128MiB
    难度
    2
    标签
    递交数
    77
    已通过
    46
    上传者