【问题标题】:How to find the maximum product of two prime numbers in an array?如何找到数组中两个素数的最大乘积?
【发布时间】:2017-01-08 19:31:32
【问题描述】:
#include <stdio.h>
int final[3];
int checkprime(int n,int loopcount)
{
    int flag=0,m;
    for(m=2; m<=loopcount; m++)
    {
        if(n%m==0 && n!=2)
        {
            flag=1;
            return 0;
            break;

        }
        else if(n==2)
        {
            flag=0;
            break;

        }
    }
    if(flag==0)
    {
        return 1;
    }
}
int main()
{
    int test_no,n,i,j,k,m,max=1,loopcount,product=1,max_available=0,count=0;
    scanf(" %d",&test_no);
    for(i=0; i<3; i++)
    {

        scanf(" %d",&n);
        int array[n];
        for(j=0; j<n; j++)
        {
            scanf(" %d",&array[n]);
            loopcount=array[n]/2;
            if(max<loopcount)
            {
                max=loopcount;
            }
        }
        loopcount=max;
        max=array[0];
        for(j=0; j<n; j++)
        {
            int x=checkprime(array[j],loopcount);
            if(x==1)
            {
                if(array[j]>=max)
                {
                    max=array[j];
                }
            }
        }
        product=product*max;
        max=1;
        for(j=0; j<n; j++)
        {
            int x=checkprime(array[j],loopcount);
            if(x==1)
            {
                if(array[j]>max && product!=array[j])
                {
                    max=array[j];
                    max_available=1;
                }
                else if(array[j]>=max && product==array[j] && max_available==0)
                {
                    max=product;
                }
            }
            if(x==0)
            {
                count++;
            }
        }
        if(count==n || count==n-1)
        {
            final[i]=-1;

        }
        else
        {
            product=product*max;
            final[i]=product;

        }
        product=1;
    }
    for(i=0; i<3; i++)
    {
        printf("%d\n",final[i]);
    }
    return 0;
}

上面的代码不能正常工作,我无法得到这背后的原因,我找到了两次素数,我使用了一个变量 loopcount 来找到迭代次数来检查数字是否是素数与否。

我最初采用了一个大小为 3 的数组,我提供了 3 个输入,每个输入具有不同的数组大小,然后我迭代两次以找到最大乘积,如果我没有找到任何素数,我输出 -1在输出屏幕上。 第一行描述了输入的总数,可以看作是测试用例的数量,对于每个测试用例,第一行描述数组的大小,第二行描述数组中存在的元素。请帮助我识别问题,对于第一个输入,它打印 -1 但有一些问题,理想情况下,它应该只转到 if 部分

if(count==n || count==n-1)
{
    final[i]=-1;         
}

但它会编码else部分的sn-p,

else
{
    product=product*max;
    final[i]=product;
}

我通过在 else 部分编写 printf 语句来检查这一点,因此我能够为第一个输入打印值,这很令人困惑,因为当 if 部分被执行时,为什么 else 部分被执行?对于其余的输入,它正在打印垃圾值。

这是一个输入集:

3

5
1 4 6 8 10

3
2 2 9

2
156 13 

对应的输出是:

-1
4
169

【问题讨论】:

  • 如果使用一致的缩进,您的代码会更容易阅读
  • 附注:检查数字是否为素数是一项昂贵的操作,因此您应该扭曲检查:首先您应该检查候选数字是否大于两个已经选择的数字中的任何一个,然后检查是否是否是素数。
  • 能否提供相应的输出??
  • 您可能希望先对数组进行排序,然后检查从高端开始的素数。这样你就可以在找到两个素数时停止检查。

标签: c algorithm primes


【解决方案1】:

您应该扫描array[j] 而不是array[n]nth 索引无效。

int array[n];
for(j=0; j<n; j++)
{
    scanf(" %d",&array[n]);  /// Problem is here
    loopcount=array[n]/2;
    if(max<loopcount)
    {
        max=loopcount;
    }
}

【讨论】:

  • 我更正了我的代码,现在我只得到了第二个输入的正确值,我已经修改了代码行 if((count==n||count==n-1)&& max_available ==0) 并且在将产品值分配给最终数组后,我再次重新分配 max_available=0 ,请检查修改后的代码,我无法追踪确切的问题,它为第一个和第三个输入打印 1 @ 987654321@
  • 好的。请您编辑您的问题并为输入集提供相应的输出?由于您的问题尚不清楚。
  • 对应输入的输出应该是 -1 , 4 和 169 ,对于第一个输入我没有素数,所以输出是 -1 ,对于第二个输入,我只有一个素数,因此相同的数字与自身相乘,因此输出为 4,对于第三个输入,我也只有 1 个素数,因此输出为 169,但我没有得到所需的输出
  • 所以,如果输入集是2 5 4 7,那么输出是35 ??
  • 是的,先生,5 和 7 的乘积
【解决方案2】:

以前,您有一个非常复杂的想法。这是对您的代码进行的一些优化。随便问什么。希望对你有帮助:)

#include <stdio.h>
#include <math.h>

int checkprime(int n)
{
    int flag=0,m;
    for(m=2; m<=sqrt(n); m++)
    {
        if(n%m==0)return 0;
    }
    return 1;
}
int main()
{
    int test_no,n,i,j,k,m,max=1,loopcount,product=1,max_available=0,count=0;
    scanf("%d",&test_no);
    for(i=0; i<test_no; i++)
    {
        scanf(" %d",&n);
        int array[n];
        for(j=0; j<n; j++)
        {
            scanf("%d",&array[j]);
        }
        sort(array);  // Use any sort algorithm you wish to sort the array
        int _1=1, _2=1;
        for(int j=n-1; j>=0; j--){
            if(checkprime(array[j])==1){
                if(_1==1){
                    _1=array[j];
                }
                else if(_1!=1 && _2==1){
                    _2=array[j];
                    break;
                }
            }
        }
        if(_1==1 && _2==1)printf("-1\n");
        else if(_2==1 && _1!=1)printf("%d\n", _1*_1);
        else printf("%d\n", _1*_2);
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 2020-09-06
    • 2017-02-02
    • 2021-05-09
    • 1970-01-01
    • 2013-12-31
    • 2017-03-01
    • 2020-08-30
    相关资源
    最近更新 更多