6 solutions

  • 13
    @ 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-11-30 16:27:35
      #include<bits/stdc++.h>
      using namespace std;
      int main(){
          int a[111110],n;cin>>n;
          for(int i=2;i<=n;i++) if(a[i]==0) for(int j=i*2;j<=n;j+=i) a[j]=1;
          for(int i=2;i<=n;i++) if(a[i]==0) cout<<i<<" ";}
      

      本题最短代码!!!

      • 0
        @ 2025-11-16 15:59:54
        #include<bits/stdc++.h>
        using namespace std;
        int a[30000];
        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;
        			}
        		}
        		if(a[i]==0){
        			cout<<i<<" ";
        		}
        	}
        	return 0;
        }
        
        
        
        • -1
          @ 2025-8-12 16:29:30
          #include<bits/stdc++.h>
          using namespace std;
          int r;
          int main(){
          	cin>>r;
          	int cnt=0;
          	for(int i=2;i<=r;i++){
          		bool flag=true;
          		for(int j=2;j<i;j++){
          			if(i%j==0){
          				flag=false;
          			}
          		}
          		if(flag==true){
          			cout<<i<<' ';
          		}else{
          			continue;
          		}
          	}
          	
          	
          	return 0;
          }
          
        • -7
          @ 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;
          }
          
          • -7
            @ 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
            150
            Accepted
            61
            Uploaded By