• PAT:1013


    #include<iostream>
    #include<math.h>
    using namespace std;
    //judge函数是用来判断一个数是否是素数,是素数函数返回1,不是返回0
    int judge(int a)
    {
    	int count = 0;
    	for (int i = 1; i <= sqrt(a); i++)
    	{
    		if (a % i == 0)
    		{
    			count++;
    		}
    	}
    	if (count > 1)
    	{
    		return 0;
    	}
    	return 1;
    }
    //count_judge函数用来寻找所需要的素数
    int count_judge(int x)//x为输入所需要的第几个素数,为5就是寻找第五个素数
    {
    	int i = 0;
    	int count = 2, save = 0;//2是第一个素数,count从2开始,save用来保存素数,找到所需要的素数时返回
    	//判断x是否越界
    	if (x == 0||x<0)
    	{
    		return 0;
    	}
    	while (i < x)
    	{
    		if (judge(count))
    		{
    			i++;
    			save = count;
    		}
    		count++;
    	}
    	return save;
    }
    int main()
    {
    	int M=0, N=0;
    	cin >> M >> N;
    	//判断输入是否越界
    	if (M>N ||M>10000||N>10000||M<0||N<0 )
    	{
    		return 0;
    	}
    	int x = 0, y = 0;
    	x = count_judge(M);//保存所需要的边界
    	y = count_judge(N);
    	int count = 0;
    	for (int i = x; i <= y; i++)
    	{
    		if (judge(i))
    		{
    			//输出格式要求
    			if ((count % 10) == 0&&count!=0)
    			{
    				cout << endl;
    			}
    			cout << i;
    			count++;
    			//输出格式要求
    			if ((count % 10) != 0 && i != y)
    			{
    				cout << " ";
    			}
    			
    		}
    		
    	}
    	return 0;
    }
    
  • 相关阅读:
    浅谈Linux进程
    POJ 1426 Find The Multiple
    JS JavaScript闭包和作用域
    JS JavaScript深拷贝、浅拷贝
    JS JavaScript中的文档碎片 DocumentFragement JS性能优化
    前端DOM知识点
    JS JavaScript中的this
    JS JavaScript实现杨辉三角
    JS JavaScript事件循环机制
    ES6基本语法
  • 原文地址:https://www.cnblogs.com/zongji/p/12253024.html
Copyright © 2020-2023  润新知