当前位置:网站首页>PTA:自测-2 素数对猜想 (20分)

PTA:自测-2 素数对猜想 (20分)

2022-06-12 14:51:00 苏木George

t

基本思路:先求出所有的素数对,然后在遍历一遍查看有多少对。
#include"stdio.h"
#include"math.h"
#include <cstdlib>
#include <iostream>
#include <queue>

using namespace std;

int sushu(int n)
{
    
	int i=2;
	if(n<2) return 0;
	for(;i<=sqrt(n);i++){
    
		if(n%i==0){
    
			return 0;
		}
	}
	return 1;
}

int main(){
    	
	queue<int> q;
	int n =0;
	scanf("%d",&n);
	int m =0;
	int s = 2;
	for (int i=3;i<=n;i++){
    
		if(sushu(i) == 1){
    
			if((i-s) == 2){
    
				m++;
			}
			s =i;
		}
		
	}
	printf("%d",m);
}
原网站

版权声明
本文为[苏木George]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44106306/article/details/103872471