[java]代码库
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int [] a = new int[num];
int i, j, k;
for(i = 2, k = 0; i < num; i++)
{
int flag = 0;
for(j = 2; j <= i/2; j++)
{
if(i % j == 0)
flag = 1;
}
if(flag == 0)
{
a[k] = i;
k++;
}
}
System.out.print(a[0]);
for(i = 1; i < k; i++)
System.out.print("," + a[i]);
input.close();
}
}
方法2:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int i, j, k = 0;
for(i = 2; i <= N; i++) {
int flag = 0;
for(j = 2; j < i; j++) {
if(i%j == 0) {
flag = 1;
}
}
if(flag == 0) {
if(k == 0)
System.out.print(i);
else
System.out.print("," +i);
k++;
}
}
scan.close();
}
}