编程介的小学生 2019-03-11 00:52 采纳率: 20.5%
浏览 674

打印输出范围内素数的个数有多少,采用C编程语言方式的实现

Problem Description
Give you a lot of positive integers, just to find out how many prime numbers there are.

Input
There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.

Output
For each case, print the number of prime numbers you have found out.

Sample Input
3
2 3 4

Sample Output
2

  • 写回答

2条回答 默认 最新

  • qq_37664296 2019-03-11 09:23
    关注

    只会java的,应该差不多吧

    Scanner sc = new Scanner(System.in);
            System.out.println("输入起始:");
            int beginNum = sc.nextInt();
            System.out.println("输入结束:");
            int endNum = sc.nextInt();
            if(endNum>beginNum)
            {
                for(int i = beginNum;i<=endNum;i++)
                {
                    boolean isPrime = true;
                    for(int j = 2;j<=Math.sqrt(i);j++)
                    {
                        if(i%j==0)
                        {
                             isPrime = false;
                             break;
                        }
                    }
                    if(isPrime)
                    {
                        System.out.println(i);
                    }
                }
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog