PAT (Basic Level) Practise (中文)1013. 数素数 (20)
来源:互联网 发布:js操作input file 编辑:程序博客网 时间:2024/05/29 10:24
1013. 数素数 (20)
时间限制
100 ms
内存限制
65536 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 4347 53 59 61 67 71 73 79 83 8997 101 103
//一样的代码java超时,用c重写一遍就好import java.util.Scanner;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int left = sc.nextInt(); int right = sc.nextInt(); int times = 0; for(int i=1 ; ;i++){ if(isPrimeNum(i)){ times++; if(times>=left×<=right){ if((times-left+1)%10==0||times-left==right-left){ System.out.println(i); }else{ System.out.printf("%d ", i); } } if(times == right){ break; } } } } public static boolean isPrimeNum(int a){ if(a==1){ return false; } if(a%2==0&&a!=2){ return false; } for(int i=3;i<=Math.sqrt(a);i+=2){ if(a%i==0){ return false; } } return true; }}//gcc编译器#include<stdio.h>#include<math.h>int isPrimeNum(int a){ if(a==1){ return 0; } if(a%2==0&&a!=2){ return 0; } for(int i=3;i<=sqrt(a);i+=2){ if(a%i==0){ return 0; } } return 1;}int main(){ int time=0; int start,end; scanf("%d %d",&start,&end); for (int i = 0; ; i++) { if (isPrimeNum(i)!=0) { time++; if (time>=start&&time<=end) { if ((time-start+1)%10==0||(time-start)==(end-start)) { printf("%d\n",i);; }else { printf("%d ",i); } } if (time==end) { break; } } } return 0;}
阅读全文
0 0
- PAT (Basic Level) Practise (中文)1013. 数素数 (20)
- PAT (Basic Level) Practise (中文) 1013. 数素数 (20)
- PAT (Basic Level) Practise (中文)1013. 数素数 (20)
- PAT (Basic Level) Practise (中文)1013. 数素数 (20)
- PAT (Basic Level) Practise (中文)1013数素数 (20)
- PAT (Basic Level) Practise (中文)1013. 数素数 (20) C语言
- PAT (Basic Level) Practise (中文) 1064. 朋友数(20)
- PAT (Basic Level) Practise (中文)1064. 朋友数(20)
- PAT (Basic Level) Practise (中文)1002.写出这个数
- PAT (Basic Level) Practise (中文)1007. 素数对猜想 (20)
- PAT (Basic Level) Practise (中文)1007. 素数对猜想 (20) C语言
- PAT (Basic Level) Practise (中文)1007素数对猜想 (20)
- PAT (Basic Level) Practise (中文) 1007. 素数对猜想 (20)
- PAT (Basic Level) Practise (中文)1007. 素数对猜想 (20)
- PAT BASIC LEVEL 1013. 数素数 (20)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)--1001
- CTF web题总结--unserizable
- @线性回归模型
- MySQL性能优化的最佳20+条经验
- Hive 之 Java API 操作
- 随笔 2017.08.29
- PAT (Basic Level) Practise (中文)1013. 数素数 (20)
- Gym
- 如何简单实现接口自动化测试—基于 python
- Android loader的简单使用
- jenv的安装配置与使用
- poj1423(斯特林公式)
- Spring实现微服务—进阶篇
- Hive 数据表重命名
- 日期时间函数