100分 求输入1个数N 打印前N个素数的代码
求输入1个数N 打印前N个素数的代码
输入N
打印前N个
[解决办法]
import java.io.*;
public class Test {
public static void printPrimeNum(int n) {
int temp =(int)(Math.sqrt(n) + 1);
for(int i = 2;i<temp; i++) {
if(n%i == 0) {
return;
}
}
System.out.println(n);
}
public static void main(String[] args)
throws IOException {
System.out.print("entry :>>");
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
for(int i = 2;i<n+1;i++) {
printPrimeNum(i);
}
}
}
[解决办法]
// 前边的兄弟可能没有看清楚题意,题目是要求输入N,输出前N个素数
// 而不是输出所有小于N的素数,这里N应为为正整数才有意义
// NPrimes.java
// jdk>=1.5
import static java.lang.System.out;
import java.util.Scanner;
import java.math.BigInteger;
public class NPrimes
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
out.println("题目:求输入1个数N 打印前N个素数 ");
out.println("输入N");
int n=Integer.parseInt(sc.next());
out.println("打印前N个");
BigInteger bi = new BigInteger("0");
BigInteger tempBi = new BigInteger("0");
for(int i=0;i<n;i++)
{
bi = tempBi.nextProbablePrime();
tempBi=bi;
out.println("第"+(i+1)+"个:"+bi);
}
sc.close();
}
}
[解决办法]
import java.util.*;
public class PrimeNumber {
public static void main(String[] args){
long count = 1;
Scanner in = new Scanner(System.in);
long n = 0;
long m = 0;
while(true){
System.out.print("请输入N的值==>");
n = in.nextLong();
if(n>1){
break;
}
}
while(true){
System.out.print("打印前M个数?==>");
m = in.nextLong();
if(m>0){
break;
}
}
System.out.print("2 ");
for(int i=3;i<=n;i+=2){
boolean flag = true;
for(int j=2;j<i;j++){
if(i%j==0){
flag = false;
}
}
if(!flag){continue;}
System.out.print(i+" ");
count++;
if(count==m){
break;
}
}
}
}
第一次发,我怎对不齐啊??????
[解决办法]
本来昨晚我是第一个回帖的,不幸写好后,网络出现问题,郁闷.
真是一个令人怀念的题目,不知道楼主是要打印前n个素数还是n前面的素数,下面程序片段楼主根据需要自己改吧,写程序完全照搬是不会有进步的
/**
* @param args
*/
public static void main(String[] args) {
int num = Integer.parseInt(readSystemIn());
for (int i = 1; i <= num; i++) {
if (isPrime(i))
System.out.println(i);
}
}
/**
* 判断一个数是否是素数
*
* @param num
* @return
*/
private static boolean isPrime(int num) {
for (int k = 2; k <= num/2; k++) {
if (num % k == 0)
return false;
}
return true;
}
/**
* 从控制台读取一串字符串
*
* @return
*/
public static String readSystemIn() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
[解决办法]
for example
public class Test {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("input a number:");
int num = scannber.nextInt();
for (int i=2, k=0;k<num; i++) {
if (isPn(i)) {
System.out.print(i + " ");
k++;
if (k%10==0) System.out.println();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
public static boolean isPn(int num) {
if (num < 2) return false;
for (int i=2; i<(int)Math.sqrt(num); i++) {
if (num%i==0) return false;
}
return true;
}
}
[解决办法]
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.lang.Math; public class PrintPrime { public static int getInput() { //取得键盘输入的数字 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = ""; int input = 0; System.out.println("请输入要打印的前N个素数的个数N:(只可以为数字)"); try { s = br.readLine(); input = Integer.parseInt(s); } catch (NumberFormatException e) { System.out.print("您输入的不是数字,请输入数字!"); e.printStackTrace(); System.exit(-1); } catch (IOException e) { System.out.print("您输入有错误,请输入数字!"); e.printStackTrace(); System.exit(-1); } return input; } public static void printFirstInputPrime(int input) { //打印前N个素数 int count = 0; int flag = 1; System.out.println("前" + input + "个素数是:(10个/行)"); for (int i = 2; count < input; i++) { double x = Math.sqrt(i + 1); for (int j = 2; j < x; j++) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) { System.out.print(i + " "); count++; if (count % 10 == 0) { System.out.println(); } } flag = 1; } } public static void main(String[] args) { int input = getInput(); printFirstInputPrime(input); }}