文件名称:2
介绍说明--下载内容均来自于网络,请自行研究使用
1. 编写筛选查找素数函数:
void sieve(bool isPrime[], int n)
其中isPrime[n]为一个布尔型数组,n为数组大小。由于2是第一个素数,所以设置isPrime[0]和isPrime[1]的值为false,并设置其余的元素初值为true。然后对从4到n-1的每一个i,判断i是否能够被2整除,如果i能够被2整除,则设置isPrime[i]为false。对从2到n/2的每一个可能的数值重复以上处理,当操作结束时,所有值为true的isPrime[i]所对应的i就是素数,它们将是从筛子中掉下来的。
测试程序:
输入:一个整数,表示n
输出:1行,1到n之间的所有素数,两个素数之间用空格隔开,最后一个素数输出后,输出一个空格和空行,输出参见函数Disp的代码。
例如,当输入20时,输出如下:
2 3 5 7 11 13 17 19
-1. Preparation of screening to find primes function: void sieve (bool isPrime [], int n) where isPrime [n] is a Boolean array, n is the array size. Because 2 is the first prime number, so set isPrime [0] and isPrime [1] the value is false, and set the rest of the elements of the initial value to true. Then 4 to n-1 every i, i determine whether it can be divisible by 2, if i can be divisible by 2, set isPrime [i] is false. For every possible value 2 to n/2 Repeat the above process, when the end of the operation, all the value of true isPrime [i] corresponding to the i is a prime number, they will be falling the sieve come. Test Procedure: Input: An integer representing the n output: 1 line, all the prime numbers between 1 and n, separated by a space between two prime number, a prime number after the last output, the output of a blank and blank lines, see the output function Disp code. For example, when the input 20, the output is as follows: 235711131719
void sieve(bool isPrime[], int n)
其中isPrime[n]为一个布尔型数组,n为数组大小。由于2是第一个素数,所以设置isPrime[0]和isPrime[1]的值为false,并设置其余的元素初值为true。然后对从4到n-1的每一个i,判断i是否能够被2整除,如果i能够被2整除,则设置isPrime[i]为false。对从2到n/2的每一个可能的数值重复以上处理,当操作结束时,所有值为true的isPrime[i]所对应的i就是素数,它们将是从筛子中掉下来的。
测试程序:
输入:一个整数,表示n
输出:1行,1到n之间的所有素数,两个素数之间用空格隔开,最后一个素数输出后,输出一个空格和空行,输出参见函数Disp的代码。
例如,当输入20时,输出如下:
2 3 5 7 11 13 17 19
-1. Preparation of screening to find primes function: void sieve (bool isPrime [], int n) where isPrime [n] is a Boolean array, n is the array size. Because 2 is the first prime number, so set isPrime [0] and isPrime [1] the value is false, and set the rest of the elements of the initial value to true. Then 4 to n-1 every i, i determine whether it can be divisible by 2, if i can be divisible by 2, set isPrime [i] is false. For every possible value 2 to n/2 Repeat the above process, when the end of the operation, all the value of true isPrime [i] corresponding to the i is a prime number, they will be falling the sieve come. Test Procedure: Input: An integer representing the n output: 1 line, all the prime numbers between 1 and n, separated by a space between two prime number, a prime number after the last output, the output of a blank and blank lines, see the output function Disp code. For example, when the input 20, the output is as follows: 235711131719
(系统自动生成,下载前可以参看下载内容)
下载文件列表
2\3.cpp
.\c++_ch06_03.vcxproj
.\c++_ch06_03.vcxproj.filters
.\Debug\3.obj
.\.....\c++_ch06_03.lastbuildstate
.\.....\c++_ch06_03.log
.\.....\CL.read.1.tlog
.\.....\CL.write.1.tlog
.\.....\link-cvtres.read.1.tlog
.\.....\link-cvtres.write.1.tlog
.\.....\link-rc.read.1.tlog
.\.....\link-rc.write.1.tlog
.\.....\link.2484-cvtres.read.1.tlog
.\.....\link.2484-cvtres.write.1.tlog
.\.....\link.2484-rc.read.1.tlog
.\.....\link.2484-rc.write.1.tlog
.\.....\link.2484.read.1.tlog
.\.....\link.2484.write.1.tlog
.\.....\link.4604-cvtres.read.1.tlog
.\.....\link.4604-cvtres.write.1.tlog
.\.....\link.4604-rc.read.1.tlog
.\.....\link.4604-rc.write.1.tlog
.\.....\link.4604.read.1.tlog
.\.....\link.4604.write.1.tlog
.\.....\link.read.1.tlog
.\.....\link.write.1.tlog
.\.....\vc110.idb
.\.....\vc110.pdb
.\Debug
2