PKUOJ 区间内的真素数

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;
}

  

posted @ 2019-01-14 21:44  唐淼  阅读(356)  评论(0编辑  收藏  举报