解题思路:
我想说的是这题不对。
1不是素数,但如果按编译正确的话。结果1是素数
注意事项:
正确代码如下,不过这个代码不能通过系统检测。这里有两个做法。可以通过系统检测的程序,我也发下。是程序3,就是有错误不符合规则。
参考代码:程序1
import java.util.*;
public class z1029 {
public static void t(int n) {
int s = 0;
if (n == 1)
System.out.print("not prime");
else {
for (int i = 2; i < n; i++) {
if (n % i == 0)
s++;
}
if (s == 0)
System.out.print("prime");
else
System.out.print("not prime");
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
t(n);
}
}
程序2
public class z1029 {
public static boolean t(int n) {
if (n == 1)
return false;
else {
for (int a = 2; a < n; a++)
if (n % a == 0)
return false;
return true;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (t(a))
System.out.print("prime");
else
System.out.print("not prime");
}
}
程序3
import java.util.*;
public class z1029 {
public static void t(int n) {
int s = 0;for (int i = 2; i < n; i++) {
if (n % i == 0)
s++;
}
if (s == 0)
System.out.print("prime");
else
System.out.print("not prime");
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
t(n);
}
0.0分
0 人评分
Hello, world! (C语言代码)浏览:1201 |
C语言训练-求素数问题 (C语言代码)浏览:728 |
C语言程序设计教程(第三版)课后习题9.2 (Java代码)浏览:636 |
WU-蓝桥杯算法提高VIP-交换Easy (C++代码)浏览:1119 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:835 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:518 |
字符逆序 (C语言代码)浏览:621 |
GC的苦恼 (C语言代码)浏览:621 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:785 |
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:667 |