1013. 数素数 (20)

令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。

输入格式:

输入在一行中给出M和N,其间以空格分隔。

输出格式:

输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。

输入样例:

5 27
输出样例:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103

// 注:测试点4始终没通过,不知道是什么原因,如果各位有知道的,麻烦指点一下,谢谢。


#include "iostream"
#include "vector"
#include "math.h"
//#include "stdio.h"
using namespace std;

int main()
{
	int order_start = 0, order_end=0;
	int count = 0;
	vector<int> output;
	bool flag = false;
	cin >> order_start >> order_end;

	for (int i = 2; i <= 10000; i++)
	{
		flag = true;
		if (i % 2 == 0 && i != 2)
		{
			flag = false;
			continue;
		}
		for (int j = 2; j <= sqrt(i); j++)
		{
			if (i%j == 0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			count++;
			if (count >= order_start&&count <= order_end)
			{
				output.push_back(i);
				if (count == order_end)
					break;
			}
				
		}
	}

	for (int i = 0; i < output.size(); i++)
	{
		if ((i+1) % 10 == 0)
			cout << output[i] << endl;
		else
		{
			if (i < output.size() - 1)
				cout << output[i] << " ";
			else
				cout << output[i];
		}
	}
	system("pause");
	return 0;
}

// 判断素数的算法没有问题,修改了程序如下,测试通过。

for循环那里出现的问题,但是不知道到底是为什么?

参考链接:http://www.th7.cn/Program/cp/201505/445775.shtml


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
bool prime(int value)
{
	if (value % 2 == 0 && value != 2)
	{
		return false;
	}
	for (int j = 2; j <= sqrt(value); j++)
	{
		if (value%j == 0)
		{
			return false;
		}
	}
	return true;
}
int main()
{ 
	int order_start = 0, order_end = 0;
	int count = 0;
	vector<int> output;

	cin >> order_start >> order_end;

	int figure = 2;
	while (count <= order_end)
	{
		if (prime(figure))
		{
			count++;
			if (count >= order_start&&count <= order_end)
			{
				output.push_back(figure);
			}
		}
		figure++;
	}

	for (int i = 0; i < output.size(); i++)
	{
		if ((i + 1) % 10 == 0)
			cout << output[i] << endl;
		else if (i < output.size() - 1)
			cout << output[i] << " ";
		else
			cout << output[i];

	}
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值