使用开方是重点。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); //输入N int count = 0; for (int i = 1; i <= N; i++) { //统计1-N中符合条件的 if (fun(i)){ count++; } } System.out.println(count); } //判断数是否符合条件 public static boolean fun(int x){ int n = (int) Math.sqrt(x); //(重要!!!) for (int i = 2; i <= n; i++) { if (x%i==0 && x/i!=i){ return true; } } return false; } }