码迷,mamicode.com
首页 > 其他好文 > 详细

PKUOJ 区间内的真素数

时间:2019-01-14 22:01:18      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:sys   cout   temp   namespace   cpp   math.h   using   div   class   

http://bailian.openjudge.cn/tm2018/A/

#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;

bool isPrime(int n)
{
	for (int i = 2; i <=sqrt(n); i++)
	{
		if (n%i == 0)
		{
			return false;
		}
	}
	return true;
}


int Reverse(int n)
{
	std::string temp;
	std::stringstream ss;
	ss << n;
	ss >> temp;
	reverse(temp.begin(), temp.end());
	int t = atoi(temp.c_str());
	return t;
}

int main()
{
	int M, N;
	cin >> M >> N;
	std::string c;
	for (int i = M; i <= N; i++)
	{
		if (isPrime(i) && isPrime(Reverse(i)))
		{
			stringstream ss;
			string temp;
			ss << i;
			ss >> temp;
			c=c+temp+",";
		}
	}
	if (c.size()<1)
	{
		std::cout << "No";
	}
	else
	{
		for (int i = 0; i < c.size()-1;i++)
		{
			std::cout << c[i];
		}
	}
	//system("pause");
	return 0;
}

  

PKUOJ 区间内的真素数

标签:sys   cout   temp   namespace   cpp   math.h   using   div   class   

原文地址:https://www.cnblogs.com/tangmiao/p/10269229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!