import java.io.*; /** * 筛法求素数 */ public class Main { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out)); static boolean[] judgeNumber(int n) { boolean[] f = new boolean[n + 1]; f[1] = false; for (int i = 2; i < n; i++) { // 把从 2 开始的数全部初始化 f[i] = true; } f[0] = false; // 0 无意义,置为 false for (int i = 2; i <= n; i++) { // 质数的倍数(1 倍数除外)必然是合数 if (f[i] == true) { for (int j = 2; j * i <= n; j++) { f[i * j] = false; } } } return f; } public static void main(String[] args) throws Exception { cin.nextToken(); int n = (int) cin.nval; boolean[] b = judgeNumber(n); for (int i = 0; i < b.length; i++) { if (b[i] == true) { cout.println(i); } } cout.flush(); } }
0.0分
0 人评分
C二级辅导-计负均正 (C语言代码)浏览:652 |
printf基础练习2 (C语言代码)浏览:605 |
回文串 (C语言代码)浏览:3096 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:672 |
DNA (C语言描述,数据结构)浏览:909 |
IP判断 (C语言代码)浏览:820 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:597 |
永远的丰碑 (C语言代码)浏览:608 |
C语言程序设计教程(第三版)课后习题12.3 (C语言代码)浏览:587 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:476 |