Questions related to prime numbers

Question A: Prime numbers

http://codeup.cn/problem.php?cid=100000591&pid=0
Insert picture description here
Insert picture description here

#include<cstdio>
bool a[10005]={
    
    false};
int main(void)
{
    
    
	a[2]=false;
	for(int i=2;i<=10005;i++)
	{
    
    
		if(a[i]==false)
		for(int j=i+i;j<=10005;j=j+i)
		{
    
    
			a[j]=true;
		}
	}
	int n;
	while( scanf("%d",&n) != EOF)
	{
    
    
		bool flag=false;
		for(int i=2;i<n;i++)
		{
    
    
			if(!a[i])
			{
    
    
				if(i%10==1)
				{
    
    
					flag=true;
					printf("%d ",i);
				}
			}
		}
		if(!flag)
			printf("-1");
		printf("\n");
	} 
	return 0;
}

Question B: Prime Number

http://codeup.cn/problem.php?cid=100000591&pid=1
Insert picture description here
Insert picture description here
Meaning of the question: output the k-th prime number

#include<cstdio>
bool a[10000005]={
    
    false};
int main(void)
{
    
    
	a[2]=false;
	for(int i=2;i<=10000005;i++)
	{
    
    
		if(a[i]==false)
		for(int j=i+i;j<=10000005;j=j+i)
		{
    
    
			a[j]=true;
		}
	}
	int n;
	while( scanf("%d",&n) != EOF)
	{
    
    
		bool flag=false;
		int count=0;
		for(int i=2;i<10000005;i++)
		{
    
    
			if(!a[i])
			{
    
    
				count++;
				if(count==n)
				{
    
    
					flag=true;
					printf("%d",i);
					break;
				}
			}
		}
		if(!flag)
			printf("-1");
		printf("\n");
	} 
	return 0;
}

Question C: Goldbach conjecture

http://codeup.cn/problem.php?cid=100000591&pid=2
Insert picture description here
Insert picture description here
Note that:
10=3+7
10=5+5

The following is a dumb method, which can only be said to be too shabby.
I thought that if 10=5+5 were to use dual pointers, I had to write another judgment condition in it, but I
found out that it was not used at all. left<=right In the end, both pointers point to the same number, which is 5

#include<cstdio>
bool a[100005]={
    
    false};
int b[10000]={
    
    0};
int main(void)
{
    
    
	a[2]=false;
	int k=0;
	for(int i=2;i<=100005;i++)
	{
    
    
		if(a[i]==false)		
		for(int j=i+i;j<=100005;j=j+i)
		{
    
    
			a[j]=true;
		}
	}
	for(int i=2;i<=100005;i++)
	{
    
    
		if(a[i]==false)
			b[k++]=i;	
	}
	int n;
	while(1)
	{
    
    
		 scanf("%d",&n);
		 int count=0;
		 if(n==0)
		 	break;
		 int left,right;
		 left=0;
		 right=k-1;
		 bool flag=false;
		 if(n==4)
		 	printf("1\n");
		 	else
		 {
    
    
		 while(left<=right&&n!=4)
		 {
    
    
		 		if(b[left]*2==n)
		 		{
    
    
		 			flag=true;
		 			count++;
		 		}	
		 		if(b[left]+b[right]==n)
		 		{
    
    
		 			count++;
		 			right--;
		 			left--;
		 		}
		 		if(b[left]+b[right]<n)
		 		{
    
    
		 			left++;
		 		}
		 		if(b[left]+b[right]>n)
		 		{
    
    
		 			right--;
		 		}
		 }
		 if(!flag)
		 	printf("%d\n",count);
		 else
			 printf("%d\n",count-1);
		}
	} 
	return 0;
}

Simple writing:

#include<cstdio>
bool a[100005]={
    
    false};
int b[10000]={
    
    0};
int main(void)
{
    
    
	a[2]=false;
	int k=0;
	for(int i=2;i<=100005;i++)
	{
    
    
		if(a[i]==false)		
		for(int j=i+i;j<=100005;j=j+i)
		{
    
    
			a[j]=true;
		}
	}
	for(int i=2;i<=100005;i++)
	{
    
    
		if(a[i]==false)
			b[k++]=i;	
	}
	int n;
	while(1)
	{
    
    
		 scanf("%d",&n);
		 int count=0;
		 if(n==0)
		 	break;
		 int left,right;
		 left=0;
		 right=k-1;
		 bool flag=false;
		 while(left<=right)
		 {
    
    
		 		if(b[left]+b[right]==n)
		 		{
    
    
		 			count++;
		 			right--;
		 			left--;
		 		}
		 		if(b[left]+b[right]<n)
		 		{
    
    
		 			left++;
		 		}
		 		if(b[left]+b[right]>n)
		 		{
    
    
		 			right--;
		 		}
		 }
		printf("%d\n",count);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_46527915/article/details/115055802