我的代码的结果是零,我不知道为什么

问题描述 投票:-4回答:1

问题是打印出最小的素数但比其他非素数大。经过几个小时的工作后,无论我输入什么内容,我的代码都会打印出来。我哪里出错了?

(我昨天发布了这个,但由于我缺乏沟通能力,似乎没有人弄清楚问题,我更加努力,希望今天会有所不同)。

例如:4 7 8 11则结果为11,因为11是最小的素数,大于最大的非素数(即8)。这是我的代码:

#include <iostream>
#include <cmath>
#include <complex>
using namespace std;

void TypeIn(int a[] ,int &n)
    {
            cout<< "\nType in n: ";
            cin >> n;
            for(int i=0; i<n; i++)
                {
                cout << "a[" << i << "]= ";
                cin >> a[i];
                }
    }
int CheckPrimeNum(int Number)
{
    int Count=0;
    int Divisor =1;
    while (Number >= Divisor)
    {
        if(Number % Divisor == 0)
        {
            Count++;
        }
    Divisor++;
    }
    return Count;
}
int BiggestNotPrime(int a[], int n)
{
    int BiggestNotPrime =0;
    for( int i=0; i<n; i++)
    {
        if( CheckPrimeNum(a[i]) !=2)
        {
        BiggestNotPrime = a[i];
        break;
        }
    }
    if(BiggestNotPrime ==0)
    {
        return 0;
    }
    else
    {
    for( int i=0; i<n; i++)
    {
        if(CheckPrimeNum(a[i])!=2 && a[i] > BiggestNotPrime)
        BiggestNotPrime =a[i];
    }
    return BiggestNotPrime;
    }
}
int main()
{
    int n;
    int a[100];
    TypeIn(a,n);
    int SmallestPrimeLocation =0;
    for(int i=0; i<n; i++)
    {
        if(CheckPrimeNum(a[i])==2 && a[i]> BiggestNotPrime(a,n))
            SmallestPrimeLocation =i;
            break;
    }
    if(SmallestPrimeLocation ==0)
    {
        cout << 0;
    }
    else
    {
        for(int i=SmallestPrimeLocation; i<n; i++)
        {
            if(a[i]>BiggestNotPrime(a,n) && a[i] < a[SmallestPrimeLocation] && CheckPrimeNum(a[i])==2)
            {
                SmallestPrimeLocation=i;
            }
        }
        cout << a[SmallestPrimeLocation];
    }
    return 0;
}
c++
1个回答
0
投票

我发现了2个错误 - 你的答案总是为0,因为在数组中[4,7,8,9] 9是最大的非素数,并且数组中没有大于9的素数。如果输入数组为[4,7,8,9,11],则输出为11,因为11是大于9的最小素数。

第二个错误而不是使用它

if(CheckPrimeNum(a[i])==2 && a[i]> BiggestNotPrime(a,n))
            SmallestPrimeLocation =i;
            break;

你应该使用

 if(CheckPrimeNum(a[i])==2 && a[i]> BiggestNotPrime(a,n))
 {       SmallestPrimeLocation =i;
         break;
 }

这是因为如果条件和循环只执行一次,那么break语句就不在了。

但是你复杂的一个简单的任务,一个更简单的代码可以是这样的工作代码 -

#include <bits/stdc++.h>
using namespace std;

void TypeIn(int a[] ,int &n)
    {
            cout<< "\nType in n: ";
            cin >> n;
            for(int i=0; i<n; i++)
                {
                cout << "a[" << i << "]= ";
                cin >> a[i];
                }
    }
bool isPrime(int n) 
{ 
    // Corner case 
    if (n <= 1)  return false; 

    // Check from 2 to n-1 
    for (int i=2; i<n; i++) 
        if (n%i == 0) 
            return false; 

    return true; 
} 
int BiggestNotPrime(int a[], int n)
{
    int BiggestNotPrimeNum =0;
    for( int i=0; i<n; i++)
    {
        if(!isPrime(a[i]))
        {
             BiggestNotPrimeNum = a[i];
        }
    }

    return BiggestNotPrimeNum;
}

int main()
{

    int n;
    int a[100];
    TypeIn(a,n);
    //sorting the array is important
    sort(a,a+n);
    int SmallestPrimeLocation =-1;
    int BiggestNotPrimeNum =  BiggestNotPrime(a,n);
    for(int i=0; i<n; i++)
        {
        if(a[i]> BiggestNotPrimeNum && isPrime(a[i]))
        {
             SmallestPrimeLocation =i;
            break;
        }

    }
    if(SmallestPrimeLocation ==-1)
    {
        cout << "No number found"<<endl;
    }
    else
    {
        cout<<a[SmallestPrimeLocation]<<endl;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.