为什么我的程序不能识别素数
Why wont my program identify prime numbers?
我使用"橡皮擦筛"方法来制作一个程序,打印1000以内的所有素数。程序正在运行,但由于某些原因,程序不会删除复合数字。由于我的程序运行,我确信这只是一个逻辑错误,错误在我的identityPrimes函数中,但我一直无法找到它。
#include <cstdlib>
#include <iostream>
using namespace std ;
void initializeNumbers ( char number[], int ARRAY_SIZE )
{
number[0] = 'I' ; // 'I' means Ignore
number[1] = 'I' ;
for ( int i = 2 ; i < ARRAY_SIZE ; i ++ )
number[i] = 'U' ;
/* --------------------------------------------------------
Function indexOfLeastU returns the least index such that
the character stored at that index is 'U', with the
exception that -1 is returned if no array element has
value 'U'.
-------------------------------------------------------- */
int indexOfLeastU ( char number[], int ARRAY_SIZE )
{
for ( int i = 0 ; i < ARRAY_SIZE ; i ++ )
if ( number[i] == 'U' )
return i ;
return -1 ;
} // end indexOfLeastU function
/* --------------------------------------------------------
Function identifyPrimes identifies which numbers are
prime by placing 'P's at those indices.
Composite #'s are marked with 'C's.
-------------------------------------------------------- */
void identifyPrimes ( char number[], int ARRAY_SIZE )
{
int leastU = indexOfLeastU ( number, ARRAY_SIZE ) ;
while ( leastU >= 0 )
{
number [leastU] = 'P' ; // 'P' for Prime
// mark multiples as Composite ...
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
leastU = indexOfLeastU ( number, ARRAY_SIZE ) ;
} // end while loop
} // end identifyPrimes function
/* --------------------------------------------------------
Function printPrimes prints those array indices whose
corresponding elements have the value 'P'.
-------------------------------------------------------- */
void printPrimes ( char number[], int ARRAY_SIZE )
{
// print the indices at which a 'P' is stored ...
cout << "nThe prime numbers up to 1000 are:nn" ;
for ( int i = 0 ; i < ARRAY_SIZE ; i ++ )
if ( number[i] == 'P' )
cout << i << 't' ;
cout << endl << endl ;
} // end printPrimes function
int main ( )
{
// declare & initialize constants ...
const int MAX_NUMBER = 1000 ;
const int ARRAY_SIZE = MAX_NUMBER + 1 ;
// declare array ...
char number [ ARRAY_SIZE ] = { ' ' } ;
initializeNumbers ( number, ARRAY_SIZE ) ;
identifyPrimes ( number, ARRAY_SIZE ) ;
printPrimes ( number, ARRAY_SIZE ) ;
system("pause");
} // end main function
问题就在这里:
// mark multiples as Composite ...
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
任务应该是:
number[i] = 'C';
这里有一个问题。
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C'
应该是
number[i] = 'C';
首先,应该用链表(可以使用std::list)来实现它,而不是使用ignor符号。然后您可以删除您现在指定要忽略的元素。
这个程序(至少如这里所示)不会编译,因为您忘记了initializeNumbers
的右括号。
接下来,你需要修复这个循环:
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
您需要使用i
而不是leastU
相关文章:
- 为什么模板<类型名...>不能通过模板<模板类型名>识别为可实例化<typename>?
- 将参数类为可选函数指针,但不能识别为函数
- 为什么我的程序不能识别素数
- 有人可以指出为什么我的变量无法识别以及为什么我不能重载我的运算符吗
- 我得到两个错误,但我不能识别它们
- g++编译器不能识别嵌套模板类
- c++:类的成员不能被识别
- 结构由于某种原因不能识别成员
- Qt:继承的信号不能在子类中识别
- 函数的返回值不能被识别为左值
- Std::is_function不能将模板参数识别为函数
- c++: code::块不能识别包含的头文件
- Std::stoi不能被eclipse识别
- 链接器不能识别某些#include
- build build.bat不能被识别为程序
- 在调试c++程序时,QTCreator不能识别可执行文件
- CLion不能识别SDL2_image
- 不能正确识别作为构造函数参数传递的函数
- QML不能识别c++函数
- Eclipse不能识别我的GCC