当前位置:网站首页>P5723 [deep base 4. example 13] prime number pocket

P5723 [deep base 4. example 13] prime number pocket

2022-06-10 20:35:00 &volume

Catalog

subject :

Input format

Output format

I/o sample

Code :

Knowledge summary :


subject :

Small A There is a prime pocket , It can hold all prime numbers . He from  22  Start , Judge whether each natural number is a prime number in turn , If it's a prime number, you'll put this number in your pocket . The load of the pocket is the sum of all the numbers in the pocket . But the weight of the pocket is limited , You can't fit more than  LL(1 \le L \le {10}^51≤L≤105) The prime number of . give  LL, How many prime numbers can you hold in your pocket ? Output these prime numbers from small to large , Then output the maximum number of prime numbers that can be installed , There is a blank line between all the numbers .

Input format

One positive integer per line  LL.

Output format

Output these prime numbers from small to large , Then output the maximum number of prime numbers that can be installed , There is a blank line between all the numbers .

I/o sample

Input #1 Copy

100

Output #1 Copy

2
3
5
7
11
13
17
19
23
9

Code :

# include <stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	if(n<2)
	{
	    printf("0");
	    return 0;
	}
	
		int y=3;
		int s=2,i,c=1;
		printf("%d\n",s);
	while (1)
	{
		for(i=2;i<y;i++)
		{
//			printf("kkkkkkkkkkkkkkkkk");
			if(y%i==0)
			{
				y++;
//			printf("%d",y);
				break;
			}
		}        // Judge whether the prime number is no  

		if(i==y)
		{//printf("kkkkkkkkkk");
	//	printf("%d %d %d",s,y,n);
			if(s+y<=n)
			{	
					s+=y;
					printf("%d\n",y);
					y++;
					c++;
			
				
			}
			else
			{
				printf("%d",c);
				break;
			}
			
		}
	}
	return 0;
 } 

Knowledge summary :

Just a judging prime number , Focus on less than 2 In special circumstances   ( There are tests in the code , Please ignore )

原网站

版权声明
本文为[&volume]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101928294102.html