1 条题解

  • 0
    @ 2023-6-11 12:15:51

    C :

    #include<stdio.h>
    int main()
    {	
    	
    	int x;
    	while(1==1){
    		scanf("%d",&x);
    		if(x!=0){
    			int i,s=0;	
    				for(i=2;i<=x;i++){
    					int c=0;
    					int n;
    					for(n=2;n<=i/2;n++){
    						if(i%n==0){
    							c++;
    							break;
    						}
    						
    					}
    					if(c==0){
    							s++;
    						}	
    				}
    				printf("%d ",s);
    		}else{
    			break;
    		}
    	
    		
    	}
    	
    	
    	return 0;
    	
    }
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	//c统计素数个数,f判断素数 
    	int a[1100],n=0,c;
    	bool f;
    	while (1==1)
    	{
    		cin>>a[n];
    		if (a[n]==0){
    			break;
    		}
    		n++;
    	}
    	//i是下标 
    	for (int i=0;i<n;i++)
    	{
    		c=0; 
    		
    		for(int j=2;j<=a[i];j++) 
    		{
    			f=true;
    			for (int k=2;k<=sqrt(j);k++) 
    			{
    				if (j%k==0) 
    				{
    					f=false;
    					break;
    				}
    			}
    			if (f==true) c++;
    		}
    		cout<<c<<endl;
    	}
    	return 0;
    }
    

    Python :

    import math
    a = []
    b = []
    x = int(input())
    while x != 0:
         a.append(x)
         x = int(input())
    l = len(a)
    
    
    for i in range(0,l):
         num = 0
         for j in range(1,a[i]+1):
              f = True
              for k in range(2,int(math.sqrt(j))+1):
                   if j % k == 0:
                        f = False
                        break
              if j != 1 and f == True:
                   num += 1
         b.append(num)
    
    for l in b:
         print(l)
    
    • 1

    信息

    ID
    2096
    时间
    1000ms
    内存
    64MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者