zoukankan      html  css  js  c++  java
  • 编程题目: PAT 1013. 数素数 (20)

    1013. 数素数 (20)

    时间限制
    100 ms
    内存限制
    32000 kB
    代码长度限制
    8000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    令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
            又是素数相关的题目,看来真的有必要好好看看素数筛选、素数判断的高效算法了。但是在这道题目的处理中,采用较为暴力的方式也是可以处理的。直接上代码。吐槽一句,调输出格式简直烦死我了 。 = =#

    /*
    http://pat.zju.edu.cn/contests/pat-b-practise/1013 1013. 数素数 (20)
    */
    #include<cmath>
    #include<iostream>
    using namespace std;
    bool isPrime(int a)
    {
    	if(a==1 || a==0)
    		return false;
    	if(a==2)
    		return true;
    	if(a%2 == 0)
    		return false;
    	int temp = (int)sqrt((double)a);
    	for(int i = 3;i<=temp;i++)
    		if(a%i ==0 )
    			return false;
    	return true;
    }
    int main()
    {
    	int left,right;
    	cin>>left>>right;
    	int i = 1;
    	int count = 0;//计数素数
    	while(1)
    	{
    		i++;
    		if(isPrime(i))
    			count++;
    		if(count==left)
    			break;
    	}
    	while(count<right)
    	{
    		cout<<i;
    		if((count-left)%10!=9)
    			cout<<" ";
    		i++;
    		while(!isPrime(i))
    			i++;
    		count++;
    		if((count-left)%10==0)
    			cout<<endl;
    	}
    	cout<<i;
    
    	system("pause");
    	return 0;
    }




  • 相关阅读:
    前端Ajax/JS/HTML+后端SpringMVC(二)
    前端Ajax/JS/HTML+后端SpringMVC(一)
    Redis 简介及应用
    项目中使用 MyBatis(二)
    L2d插件
    [转载] 栈内存和堆内存
    Hbase排错
    matplotlib中文乱码
    cocos2dx 一些好网站
    esclipe中接入SDK时引用另一个工程或Jar
  • 原文地址:https://www.cnblogs.com/f8master/p/3826089.html
Copyright © 2011-2022 走看看