java 蓝桥杯 合数个数
问题描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
一个数如果除了 1和自己还有其他约数,则称为一个合数。例如:1, 2, 3 不是合数,4, 6 是合数。
请问从 1 到 2020 一共有多少个合数。
public class Main {
public static void main(String[] args) {
int ans=0;
for (int i = 1; i <=2020; i++) {
if(isH(i)) {
ans++;
}
}
System.out.println(ans);
}
//定义方法判断是否为合数,返回true或false
public static boolean isH(int n) {
for (int i = 2; i < n; i++) {
if(n%i==0) {
return true;
}
}
return false;
}
}