练习题目二

素数判断

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.

Write a program which reads a list of N integers and prints the number of prime numbers in the list.

Input

The first line contains an integer N, the number of elements in the list.

N numbers are given in the following lines.

Output

Print the number of prime numbers in the given list.

Constraints

1 ≤ N ≤ 10000

2 ≤ an element of the list ≤ 108

Sample Input 1

5

2

3

4

5

6

Sample Output 1

3

Sample Input 2

11

7

8

9

10

11

12

13

14

15

16

17

Sample Output 2

4

运行代码

#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int b=0,m,count;
for(int i=0;i<n;i++){
cin>>m;
count=1;
for(int i=2;i<m;i++)
{
if(m%i==0)
count=0;
break;
}
if(count==1&&m!=1)
b++;
}
cout<<b;
return 0; }

题目思考

所谓素数既是定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。所以我使用嵌套循环,使他除以2到自己本身减1的所有数,若都除不尽,则素数数量+1,。

错误及调试

第一次编写好程序进行运行,运行结果出错如图。



所以我进行调试,在调试过程中,我发现不论if里的条件成立与否,break都会运行,所以导致程序出错,调试如图。



所以,错误原因既是在if后,没有打出{ }导致if条件只对第一条代码生效,因此我修改代码后,程序运行正确。

#C++初学记录(素数判断)的更多相关文章

  1. #C++初学记录(判断子串#数学结合)

    A Count Task Problem Description Count is one of WNJXYK's favorite tasks. Recently, he had a very lo ...

  2. #C++初学记录(素数判断2)

    素数判断2 比较简单的算法,没有技术含量 A prime number is a natural number which has exactly two distinct natural numbe ...

  3. 有关素数判断的一些算法(总结&&对比)

    素性测试是数论题中比较常用的一个技巧.它可以很基础,也可以很高级(哲学).这次主要要介绍一下有关素数判断的奇技淫巧 素数的判断主要分为两种:范围筛选型&&单个判断型 我们先从范围筛选型 ...

  4. POJ 1811 大素数判断

    数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...

  5. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

  6. JAVA语言的素数判断,随机数,函数调用

    近来刚学JAVA,就从JAVA写起吧,JAVA判别素数,其实方法和C/C++没什么区别,主要就是想谈一下,其中包括的3个点. (1)JAVA语言产生随机数,random函数,定义参数max的作用是给出 ...

  7. C语言 · 素数判断

     算法提高 素数判断   时间限制:1.0s   内存限制:512.0MB      编写一函数IsPrime,判断某个大于2的正整数是否为素数. 样例输入: 5样例输出:yes 样例输入: 9样例输 ...

  8. 素数判断-----埃氏筛法&欧拉筛法

    埃氏筛法 /* |埃式筛法| |快速筛选素数| |15-7-26| */ #include <iostream> #include <cstdio> using namespa ...

  9. 初学MillerRabin素数测试

    前言 \(MillerRabin\)素数测试是一种很实用的素数判定方法. 它只针对单个数字进行判定,因而可以对较大的乃至于\(long\ long\)范围内的数进行判定,而且速度也很快,是个十分优秀的 ...

随机推荐

  1. C语言函数參数传递原理

    C语言中參数的传递方式一般存在两种方式:一种是通过栈的形式传递.还有一种是通过寄存器的方式传递的. 这次.我们仅仅是具体描写叙述一下第一种參数传递方式,第二种方式在这里不做具体介绍. 首先,我们看一下 ...

  2. day7_直播_网络编程篇(元昊老师著)

    网络编程篇计算机网络: 多台独立的计算机用网络通信设备连接起来的网络.实现资源共享和数据传递. 比如,我们之前的学过的知识可以将D盘的一个文件传到C盘,但如果你想从你的电脑传一个文件到我的电脑上目前是 ...

  3. android studio如何生成签名文件,以及SHA1和MD5值

    一.生成签名文件 1.点击菜单栏中的Build的. 2.弹出窗体,如下图,选中Generate Signed APK,并点击. 3.弹出窗体,如下图. 4.点击Create new…按钮,创建一个签名 ...

  4. iOS - UIImageView - how to handle UIImage image orientation

    本文转载至 http://stackoverflow.com/questions/8915630/ios-uiimageview-how-to-handle-uiimage-image-orienta ...

  5. C++11-新增正则表达式

    #include <regex> #include <iostream> #include <string> #include <atlstr.h> s ...

  6. 【Twitter接口】网站嵌入推特信息

    提示:要 翻 墙 的,墙内的去玩新浪微博吧 方法 1  打开链接:  https://publish.twitter.com  ,输入链接.在下边选择时间表格式 复制这段代码.粘贴到你的网站,就可以使 ...

  7. Linux学习——自定义shell终端提示符

    转自:here 我使用的Linux发行版是LinuxMint 17.2 Rafaela,默认情况下Terminal中的shell提示包括了用户名.主机名.当前目录(绝对路径)和提示符.这样会导致当进入 ...

  8. synchronized将任意对象作为对象监视器

    多个线程调用同一个对象中的不同名称的synchronized同步方法或synchronized(this)同步代码块时,调用的效果就是按顺序执行,也就是同步的,阻塞的.这说明synchronized同 ...

  9. LeetCode——Implement Queue using Stacks

    Description: Implement the following operations of a queue using stacks. push(x) -- Push element x t ...

  10. poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]

    Find the Winning Move Time Limit: 3000MS   Memory Limit: 32768K Total Submissions: 1286   Accepted:  ...