6 solutions

  • 1
    @ 2022-11-26 15:55:55
    #include<iostream>
    #include<cmath>
    using namespace std;
    
    bool isPrime(int num);
    
    int main(){
    	int n;
    	cin >> n;
    	for(int i = 1; i <= n; i++){
    		int x;
    		cin >> x;
    		if(isPrime(x)) 
    			cout << "Yes" << endl;
    		else
    			cout << "No" << endl;
    	}
    	
    	return 0;
    }
    
    bool isPrime(int num){
    	if(num <= 3)
    		return num > 1;
    	if(num%6 != 1 && num%6 != 5)
    		return false;
    	int y = (int) sqrt(num);
    	for(int i = 5; i <= y; i += 6){
    		if(num%i == 0 || num%(i+2) == 0)
    			return false;
    	}
    	return true;
    }
    
    
    • 1
      @ 2022-4-4 17:40:22

      C++

      #include <iostream>
      #include <cmath>
      using namespace std;
      int isprime(int n)
      {
          int m = sqrt(n);
          for(int i = 2; i <= m; i++)
          {
              if(n % i == 0)  return 0;
          }
          return 1;
      }
      int main()
      {
          int T = 0;
          cin >> T;
          while(T--)
          {
              int n = 0;
              cin >> n;
              if(isprime(n))  cout << "Yes" << endl;
              else cout << "No" << endl;
          }
          return 0;
      }
      
      • 0
        @ 2024-4-7 13:05:34
        #include<stdio.h>
        //判断质数 一个质数的因子只有1和它本身
        bool is_prime(int num) {
            if (num <= 1) return false; // 小于等于1的数不是质数
            if (num == 2) return true; // 2是质数
            if (num % 2 == 0) return false; // 排除偶数
        
            for (int i = 3; i * i <= num; i += 2) {
                if (num % i == 0) {
                    return false; // 如果能被除了1和自身以外的数整除,则不是质数
                }
            }
            return true; // 上述条件都不满足,则为质数
        }
        int main() {
            int n,answer;
            scanf("%d",&n);
            while (n--){
                int x;
                scanf("%d",&x);
                answer=is_prime(x);
                if (answer){
                    printf("Yes\n");
                } else{
                    printf("No\n");
                }
        
            }
            return 0;
        }
        ```
        
        • 0
          @ 2023-3-14 20:59:24

          c 用的for循环

          #include<stdio.h>
          #include<math.h>
          
          int main()
          {
          	int q,m;
          	int z;
          	int i,j,k;
          	int isp=1;
          	scanf("%d",&q);
          	int a[q];
          	for(i=0;i<q;i++){
          		scanf("%d",&m);
          		a[i]=m;
          		
          	}
          	for(j=0;j<q;j++){
          		
          		z=a[j];
          		for(k=2;k<sqrt(z);k++){
          			if(z%k==0){
          				isp=0;
          				break;
          			}
          		
          			
          		}
          		if(isp==1){
          			printf("Yes\n");
          		}
          		if(isp==0){
          			printf("No\n");
          			isp=1;
          		}
          	}
          	return 0;
          	
          }
          
          • 0
            @ 2022-3-7 20:57:29
            #include <stdio.h>
            #include <algorithm>
            #include <math.h>
            #include <vector>
            #include <iostream>
            using namespace std;
            int n;
            int main() {
                scanf("%d",&n);
                int m=0,ans=0;
                while(n--) {
                    m=0;
                    scanf("%d",&ans);
                    for(int i=2; i<=sqrt(ans); i++) //注意是小于等于
            //使用i*i<ans更好
                    {
                        if(ans%i==0) {
                            m=1;
                            printf("No\n");
                            break;
                        }
                    }
                    if(!m)
                        printf("Yes\n");
                }
                return 0;
            }
            
            
            • 0
              @ 2021-12-11 19:57:36
              #include<bits/stdc++.h>
              using namespace std;
              int  main(){
              	int a;
              	int t;
              	scanf("%d",&t);
              	for(int j=0;j<t;j++){
              		scanf("%d",&a);
              		int i,k;
              		int flag=0;
              		k=(int)sqrt((double)a);
              		for(i=2;i<=k;i++){
              			if(a%i==0){
              				flag=1;
              				break;
              			}	
              		}
              		if(flag==0){
              			printf("Yes\n");
              		}
              		else printf("No\n");
              	}
              	return 0;
              } 
              
              

              这题有点小坑,还不能直接用for(i=0;i<n;i++);麻了

              • 1

              Information

              ID
              43
              Time
              5000ms
              Memory
              256MiB
              Difficulty
              7
              Tags
              # Submissions
              1260
              Accepted
              251
              Uploaded By