4 solutions

  • 9
    @ 2024-5-5 11:35:28
    #include<bits/stdc++.h>
    using namespace std;
    const int N=31000; //数组大小 
    int a[N];
    int main()
    {
    	int n;
    	cin>>n;
    	for(int i=2;i<=n;i++) //从前往后看每个位置 
    	{
    		if(a[i]==0) //当前位置没有被标记 
    		{
    			for(int j=2*i;j<=n;j+=i) //标记i的倍数 
    			{
    				a[j]=1;
    			}
    		}
    	}
    	for(int i=2;i<=n;i++)
    	{
    		if(a[i]==0) //i没有被标记过 
    		{
    			cout<<i<<" ";
    		}
    	}
    	return 0;
    }
    
    • 1
      @ 2025-6-29 11:29:54
      #include<bits/stdc++.h>
      using namespace std;
      const int N=31000;
      int a[N];
      int main()
      {
      	int n;
      	cin>>n;
      	for(int i=2;i<=n;i++) 
      	{
      		if(a[i]==0) 
      		{
      			for(int j=2*i;j<=n;j+=i)
      			{
      				a[j]=1;
      			}
      		}
      	}
      	for(int i=2;i<=n;i++)
      	{
      		if(a[i]==0) 
      		{
      			cout<<i<<" ";
      		}
      	}
      	return 0;
      }
      
      
      • -4
        @ 2024-11-30 9:45:16
        #include<bits/stdc++.h>
        using namespace std;
        int main()
        {
            int n;
            bool a[30001];
            cin>>n;
            for(int i=1;i<=n;i++)
            {
                a[i]=true;
            }
            a[1]=false;
            for(int i=2;i<=n;i++)
            {
                if(a[i])
                {
                    for(int j=i*2;j<=n;j+=i)
                    {
                        a[j]=false;
                    }
                    cout<<i<<" ";
                }
            }
            return 0;
        }
        
        • -5
          @ 2024-6-16 15:06:27

          #include<bits/stdc++.h> using namespace std; const int N=31000; //数组大小 int a[N]; int main() { int n; cin>>n; for(int i=2;i<=n;i++) //从前往后看每个位置 { if(a[i]==0) //当前位置没有被标记 { for(int j=2*i;j<=n;j+=i) //标记i的倍数 { a[j]=1; } } } for(int i=2;i<=n;i++) { if(a[i]==0) //i没有被标记过 { cout<<i<<" "; } } return 0; }

          • 1

          Information

          ID
          62
          Time
          1000ms
          Memory
          256MiB
          Difficulty
          3
          Tags
          (None)
          # Submissions
          123
          Accepted
          49
          Uploaded By