本题的主要问题就在于运行的效率喽,主要就是要用好判断A是否是素数只要从2到根号A即可。
// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
//
//#include "stdafx.h"
#include <iostream>
#include <list>
#include <cmath>
using namespace std;
bool IfRight(int TestNum)
{
if (TestNum == 1)return false;
else
{
for (int i = 2; i<=sqrt(TestNum); i++)
{
if (!(TestNum%i)) return false;
}
return true;
}
}
int main()
{
int M = 0, N = 0;
cin >> M >> N;
int NumM = 0, NumN = 0;
int i = 1, count = 0;
int temp = 0;
bool flag = true;
list<int> TestList;
while (i>0)
{
if (IfRight(i))
{
count++;
}
if ((count == M) && flag)
{
NumM = i;
flag = false;
}
if (count == N) { NumN = i; break; }
i++;
}
for (i = NumM; i <= NumN; i++)
{
if (IfRight(i))TestList.push_back(i);
}
while (TestList.size() >= 10)
{
for (i = 0; i < 9; i++)
{
temp = TestList.front();
TestList.pop_front();
cout << temp << " ";
}
temp = TestList.front();
TestList.pop_front();
cout << temp <<endl;
}
while (TestList.size() != 1)
{
temp = TestList.front();
TestList.pop_front();
cout << temp << " ";
}
temp = TestList.front();
TestList.pop_front();
cout << temp << endl;
//system("pause");
return 0;
}