寒假刷pat乙题小计
链接:https://www.nowcoder.com/questionTerminal/e0fb49acb75f47e8b6fa2077d9071799
来源:牛客网
- 热度指数:95591 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
- 算法知识视频讲解
输入描述:
输入在一行中给出M和N,其间以空格分隔。
输出描述:
输出从PM
到PN
的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。
输入
5 27
输出
11 13 17 19 23 29 31 37 41 43<br/>47 53 59 61 67 71 73 79 83 89<br/>97 101 103
我的代码:
package 蓝桥杯寒假练习;
import java.util.Scanner;
import java.lang.Math;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m ,n ;
m = sc.nextInt();
n = sc.nextInt();
int flag = 0;
int count = 0;
for(int i = 1;i<=120000;i++) {
int flag2 = 0;
if(i>1) {
for(int j = 2;j<Math.sqrt(i)+1;j++) {
if(i%j==0&&i!=2) {
flag2 = 1;
break;
}
}
if(flag2 != 1) {
count++;
if(count >= m && count<=n) {
if(flag%10!=9) {
if(count !=n)System.out.print(i+" ");
else System.out.println(i);
}
else System.out.println(i);
flag ++;
}
}
}if(count >n)break;
}
}
}
大数除法divideToIntegralValue(取整的方法)

浙公网安备 33010602011771号