11 条题解

  • 1
    @ 2024-12-29 9:52:01

    质数只有1和它本身是他的因数

    故2~n-1无他的因数

    判断这个区间有无他的因数就行

    主题代码:

    for(int i=2;i<n;i++){
      if(n%i==0){
        printf("no");
        return 0;
      }
    }
    
    • 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;
      }
      
      • 1
        @ 2024-12-29 8:50:20

        写一个判断质数的函数即可

        #include<bits/stdc++.h>
        using namespace std; 
        int a;
        bool fun(int n){
        	if(n<=1){
        		return false;
        	}
        	for(int i=2;i<n;i++){
        		if(n%i==0){
        			return false;
        		}
        	}
        	return true;
        }
        int main(){
        	cin>>a;
        	if(fun(a)==true){
        		cout<<"yes";
        	}
        	else{
        		cout<<"no";
        	}
        	return 0;
        }
        
        • 1
          @ 2024-12-22 17:57:40

          楼下的无敌,发现大伙儿们都写函数。

          我整个对下一届初级班友好一点儿的,带注释:

          #include<bits/stdc++.h>
          using namespace std;
          int main()
          {
          	int n;
          	cin>>n;//输入,不必多说。
          	if(n==1)
          	{
          		cout<<"no";
          		return 0;
          	}
          	for(int i=2;i*i<=n;i++)//时间复杂度根号 n,算个优化,因为一旦 i 大于根号 n,n 取余 i 就不会等于 0 了。
          	{
          		if(n%i==0)
          		{
          			cout<<"no";
          			return 0;//输出完直接 return!(顺便少定义个 flag 啊哈哈哈哈)
          		}
          	}
          	cout<<"yes";
          	return 0;
          }
          
          • 1
            @ 2024-12-22 11:28:47
            #include<bits/stdc++.h>
            using namespace std;
            bool isprime(int n)
            {
            	for(int i=2;i<=sqrt(n);i++)
            	{
            		if(n%i==0)return 0;
            	}
            	return 1;
            }
            int main()
            {
            	int n;
            	cin>>n;
            	if(isprime(n))cout<<"yes";
            	else cout<<"no";
            	return 0;
            }
            
            
            • 1
              @ 2024-12-22 11:08:29

              时间复杂度 O(n)O(\sqrt n)

              #include<bits/stdc++.h>
              using namespace std;
              bool IsPrime(int n){
              	for(int i=2;i*i<=n;i++)//从 2 开始
                      if(n%i==0)return false;
              	return true;
              }
              signed main(){
              	int n;
              	cin>>n;
              	if(IsPrime(n))cout<<"yes";
              	else cout<<"no";
              }
              
              • 0
                @ 2025-5-28 21:29:47

                #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; }

                • 0
                  @ 2025-3-9 14:08:30

                  这道题太简单了

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

                  点个赞吧

                  • 0
                    @ 2024-12-29 9:03:04
                    #include<bits/stdc++.h>
                    using namespace std;
                    int n;
                    int main(){
                    	cin>>n;
                    	if(n<=1) cout<<"no";
                    	for(int i=2;i*i<=n;i++){
                    		if(n%i==0){
                    			cout<<"no";
                    			return 0;
                    		}
                    	}
                    	cout<<"yes";
                    }
                    
                    
                    • 0
                      @ 2024-12-22 11:11:04

                      直接写一个函数即可。
                      也可以直接筛。

                      #include<bits/stdc++.h>
                      #define int long long
                      #define INF 0x3f3f3f
                      using namespace std;
                      int n;
                      bool prime(int x){
                      	if(x<2)return 0;
                      	for(int i=2;i*i<=n;i++)
                      		if(!(n%i))return 0;
                      	return 1;
                      }
                      signed main(){
                      	cin>>n;
                      	if(prime(n))cout<<"yes";
                      	else cout<<"no";
                      	return 0;
                      }
                      
                      • 0
                        @ 2024-12-22 11:03:51

                        C++ :

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

                        Python :

                        # coding=utf-8
                        n=int(input())
                        flag=0
                        for i in range(2,n):
                            if n%i==0:
                                flag=1
                                print("no")
                                break
                        if flag==0:
                            print("yes")
                        
                        • 1

                        信息

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