9 条题解

  • 5
    @ 2024-1-28 18:08:54
    #include <iostream>
    using namespace std;
    int main()
    {
        int n, p, x = 0;
        cin >> n;
        for (int i = 2; i <= n; i++)
        {
            p = 1;
            for (int j = 2; j < i; j++)
            {
                if (i % j == 0)
                {
                    p = 0;
                    break;
                }
            }
            if (p == 1)
            {
                cout << i << " ";
                x += 1;
                if (x % 5 == 0)
                {
                    cout << endl;
                }
            }
        }
        return 0;
    }
    

    点赞的都是大帅哥、大美女哟!

    • 2
      @ 2023-9-26 15:17:16

      直接暴力的求素数,当然也可以是用埃氏筛或欧式筛

      #include <bits/stdc++.h>
      using namespace std;
      int isprime(int n)
      {
          if (n == 1)
              return false;
          for (int i = 2; i <= sqrt(n); i++)
              if (n % i == 0)
                  return false;
          return true;
      }
      int main()
      {
          ios::sync_with_stdio(false);
          cin.tie(0);
          cout.tie(0);
          int n, num = 0;
          cin >> n;
          for (int i = 1; i <= n; i++)
          {
              if (num == 5)
              {
                  cout << endl;
                  num = 0;
              }
              if (isprime(i))
              {
                  cout << i << " ";
                  num++;
              }
          }
          return 0;
      }
      
      • 1
        @ 2023-7-29 21:16:22
        #include <bits/stdc++.h>
        using namespace std;
        int n,a[1005],cnt;
        bool check(int x)//判断素数
        {
            if(x==1) return false;
            for(int i=2;i<x;i++)
                if(x%i==0)
                    return false;
            return true;
        }
        int main()
        {
            cin>>n;
            for(int i=1;i<=n;i++)
            {
                if(check(i))//把素数存入a数组里
                {
                    cnt++;
                    a[cnt]=i;
                }
            }
            int x;
            for(int i=1;i<=cnt;i++)
            {
                x++;
                cout<<a[i]<<" ";//输出素数
                if(x==5)//当x为5换行
                {    
                    cout<<endl;
                    x=0;//把x清零
                }
            }
            return 0;
        }
        
        • 1
          @ 2023-7-7 13:22:26

          #include <iostream>#include <cmath>usingnamespacestd; int main() { int n; cin >> n; int num = 0; for (int i = 1; i <= n; i++) { int judge = 1; if (i == 1)//1既不是质数也不是合数 { continue; } for (int j = 2; j <= sqrt(i); j++) { if (judge == 0) { continue; } if (i % j == 0) { judge = 0; } } if (judge == 1) { cout << i << " "; num++; if (num == 5) { cout << endl; num = 0; } } } return0; }

          • 0
            @ 2024-4-5 12:40:15
            #include <cmath>
            using namespace std;
            bool isPrime(int a){
            	if(a < 2){
            		return false;
            	}
            	for(int i=2;i<=sqrt(a);i++){
            		if( a%i == 0 ){
            			return false;
            		}
            	}
            	return true;
            }
            int main(){
            	int n; cin >> n; int k=0;
            	for(int p=1;p<=n;p++){
            		
            		if(k%5 == 0){
            			cout << endl;
            		}
            		if(isPrime(p) == true){
            			cout << p <<" ";
            			k++;
            		}
            	}
            	return 0;
            }
            
            • 0
              @ 2023-10-14 20:11:51
              #include<bits/stdc++.h>
              using namespace std;
              int a,sum;
              int main()
              {
                  cin>>a;
                  for(int i=2;i<a;i++)
                  {
                      int num=0;
                      for(int j=2;j<i;j++)
                      {
                          if(i%j==0)
                          {
                              num=1;
                          }
                      }
                      if(num==0)
                      {
                          if(sum==5)
                          {
                              cout<<endl;
                              sum=1;
                              cout<<i<<" ";
                          }
                          else
                          {
                              sum++;
                              cout<<i<<" ";
                          }
                          
                      }
                  }
                  return 0;
              }
              
              • 0
                @ 2023-6-9 7:04:20

                素数表

                #include<bits/stdc++.h>
                using namespace std;
                bool f[1007];
                int p[1007],num;
                int main()
                {
                    int n;
                    cin>>n;
                    memset(f,1,sizeof(f));
                    for (int i=2;i<=n;i++)
                        if (f[i])
                        {
                            p[++num]=i;
                            for (int j=i;j<=n;j+=i)
                                f[j]=0;
                        }
                    for (int i=1;i<=num;i++)
                    {
                        cout<<p[i]<<" ";
                        if (i%5==0)
                            cout<<'\n';
                    }
                    return 0;
                }
                • 0
                  @ 2023-4-22 15:05:10
                  #include <bits/stdc++.h> 
                  using namespace std;
                  int main()
                  {
                      int n, num = 0;
                      cin >> n;
                      for (int i = 2; i <= n; i++)
                      {
                          bool flag = true;
                          for (int j = 2; j < i; j++)
                          {
                              if (i % j == 0)
                              {
                                  flag = false;
                              }
                          }
                          if (flag)
                          {
                              num++;
                              if (num > 5)
                              {
                                  cout << endl;
                                  num = 1;
                              }
                              cout << i << " ";
                          }
                      }
                      return 0;
                  }
                  
                  • 0
                    @ 2022-11-3 20:27:13

                    我有一个办法:把上一题的答案搬来,改动一下即可^_^……

                    def aps(a):
                        for i in range(2,a//2+1):
                            if a%i == 0:
                                return False
                        return True#封装函数
                    a=input()
                    M=int(a)
                    p=[]
                    s=[]
                    for i in range(2,M+1):
                        if aps(i):
                            s.append(i)
                        #P64改装一下即可
                    p=0
                    for i in s:
                        if p<4:
                            print(i,end=' ')
                            p+=1
                        else:
                            print(i)#自动换行
                            p=0#一定要初始化原因DDDD
                    

                    Loading:10/100……

                  • 1

                  【入门】求出N以内的全部素数,并按每行五个数显示

                  信息

                  ID
                  65
                  时间
                  1000ms
                  内存
                  16MiB
                  难度
                  3
                  标签
                  递交数
                  215
                  已通过
                  113
                  上传者