-3

我的代码有什么问题?
我试图找到主要因素,但在输入它崩溃后它没有工作。

我现在该怎么办 ?我在生成素数后使用单独的方法生成素数我只是在 main 中调用素数函数。

#include<iostream>
#include<cstdio>
#include<math.h>
#define SIZE 1000000
using namespace std;
long p[SIZE],input;
long List[SIZE];  // saves the List
long listSize;   // saves the size of List

void prime(void)
{
       long i,j;
       p[0]=1;
       p[1]=1;
       for(i=2;i<=sqrt(SIZE);i++)  // prime generate part
            if(p[i]==0)
                for(j=2;j*i<=SIZE;j++)
                    p[i*j]=1;
}

void primeFactorize( long n )
{
    listSize = 0;   // Initially the List is empty, we denote that size = 0
    long sqrtN = long( sqrt(n) ); // find the sqrt of the number
    for( long i = 0; p[i] <= sqrtN; i++ ) { // we check up to the sqrt
        if( n % p[i] == 0 ) { // n is multiple of prime[i]
            // So, we continue dividing n by prime[i] as long as possible
            while( n % p[i] == 0 ) {
                n /= p[i]; // we have divided n by prime[i]
                List[listSize] = p[i]; // added the ith prime in the list
                listSize++; // added a prime, so, size should be increased
            }
            // we can add some optimization by updating sqrtN here, since n
            // is decreased. think why it's important and how it can be added
        }
    }
    if( n > 1 )
    {
        // n is greater than 1, so we are sure that this n is a prime
        List[listSize] = n; // added n (the prime) in the list
        listSize++; // increased the size of the list
    }
}
int main()
{
    prime();    
    while(scanf_s("%ld",&input),input)
    {
          if(input==1)
                printf("1 = 1\n");
            else if(input==-1)
                printf("-1 = -1 x 1\n");
            else
            {
                primeFactorize( input );

            }
    for( long i = 0; i < listSize; i++ ) // traverse the List array
                printf("%d ", List[i]);

    }
    return 0;
}
4

2 回答 2

1

在您的prime()例程中,您设置标志以指示数字是否为质数 - 其中带有 a1的那些不是质数。但是,在primeFactorize函数中,您假设数组p[]包含素数的值,而不是标志。所以你很快就会被零除(因为素数的标志为零),然后你就崩溃了。

您需要确保您访问的数组中包含您期望的数字!

一种可能的方法是创建两个数组:pflagspvalues. 当您第一次在prime()函数中设置标志时,您应该设置pflags. 在遍历所有可能的值之后,您可以从数组pflags中抓取零值;每次遇到一个,就将 的下一个值设置pvalues为 的索引pflags。像这样的东西:

void prime(void)
{
  long i,j;
  pflags[0]=1;
  pflags[1]=1;
  for(i=2;i<=sqrt(SIZE);i++)  // prime generate part
    if(pflags[i]==0)
      for(j=2;j*i<SIZE;j++)
        pflags[i*j]=1;
  j=0;
  for(i=0; i<SIZE; i++) {
    if(flags[i]==0) pvalues[j++]=i;
  }     
}

当然,您必须正确初始化这些数组,并在函数中使用pvalues而不是。如果你很聪明,你会发现实际上你可以对这两个东西使用相同的数组——但是要保持头脑清醒是很棘手的,所以使用两个单独的数组将有助于理解。pprimeFactorizep

编辑我决定看看我是否可以让代码运行 - 它确实可以(在修复一个错字后,将scanf_s函数更改为scanf,并将输出格式从 修复%d%ld)。我还稍微清理了 I/O。为了帮助您,我提供了适合我的完整列表:

#include<iostream>
#include<cstdio>
#include<math.h>
#define SIZE 1000000
using namespace std;
long pvalues[SIZE], pflags[SIZE], input;
long List[SIZE];  // saves the List
long listSize;   // saves the size of List


void prime(void)
{
  long i,j;
  pflags[0]=1;
  pflags[1]=1;
  for(i=2;i<=sqrt(SIZE);i++)  // prime generate part
    if(pflags[i]==0)
      for(j=2;j*i<SIZE;j++)
        pflags[i*j]=1;
  j=0;
  for(i=0; i<SIZE; i++) {
    if(pflags[i]==0) pvalues[j++]=i;
  }     
}
void primeFactorize( long n )
{
    listSize = 0;   // Initially the List is empty, we denote that size = 0
    long sqrtN = long( sqrt(n) ); // find the sqrt of the number
    for( long i = 0; pvalues[i] <= sqrtN; i++ ) { // we check up to the sqrt
        if( n % pvalues[i] == 0 ) { // n is multiple of prime[i]
            // So, we continue dividing n by prime[i] as long as possible
            while( n % pvalues[i] == 0 ) {
                n /= pvalues[i]; // we have divided n by prime[i]
                List[listSize] = pvalues[i]; // added the ith prime in the list
                listSize++; // added a prime, so, size should be increased
            }
            // we can add some optimization by updating sqrtN here, since n
            // is decreased. think why it's important and how it can be added
        }
    }
    if( n > 1 )
    {
        // n is greater than 1, so we are sure that this n is a prime
        List[listSize] = n; // added n (the prime) in the list
        listSize++; // increased the size of the list
    }
}

int main(void)
{
    prime();    
    printf("\nEnter number to factorize: ");
    while(scanf("%ld",&input),input)
    {
          if(input==1)
                printf("1 = 1\n");
            else if(input==-1)
                printf("-1 = -1 x 1\n");
            else
            {
                primeFactorize( input );

            }
    printf("The number %ld has the following factors: ", input);
    for( long i = 0; i < listSize; i++ ) // traverse the List array
                printf("%ld ", List[i]);
    printf("\n\nEnter number to factorize: ");

    }
    return 0;
}

我测试了几个不同的输入:

Enter number to factorize: 81
The number 81 has the following factors: 3 3 3 3 

Enter number to factorize: 123
The number 123 has the following factors: 3 41 

Enter number to factorize: 64
The number 64 has the following factors: 2 2 2 2 2 2 

Enter number to factorize: 0

(exits)

一切都和预期的一样。

于 2013-06-09T05:45:29.270 回答
1
void prime(void){
    long i,j;
    p[0]=1;
    p[1]=1;
    for(i=2;i<=(long)sqrt((double)SIZE);i++)
        if(p[i]==0)
            for(j=2;j*i<SIZE;j++)//<=SIZE ---> <SIZE
                p[i*j]=1;
}

void primeFactorize( long n ){
    listSize = 0;
    long sqrtN = (long)sqrt((double)n);//
    for( long i = 2; i <= sqrtN; i++ ) { //i = 0 ---> i = 2 and p[i] ---> i (Same below)
        while( n % i == 0 ) {//possible [if statement] is omitted
            n /= i;//!
            List[listSize] = i;//!
            listSize++;
        }
    }
    if( n > 1 )
    {
        List[listSize] = n;
        listSize++;
    }
}
于 2013-06-09T08:03:24.673 回答