简体   繁体   English

如何在数组中找到两个素数的最大乘积?

[英]How to find the maximum product of two prime numbers in an array?

#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;
}

The above code is not working properly ,I am unable to get the reason behind this , I am finding prime number two times and I have used a variable loopcount so as to find the number of iterations for checking whether the number is prime or not . 上面的代码不能正常工作,我无法得到这背后的原因,我找到了素数两次,我使用了一个变量loopcount,以便找到检查数字是否为素数的迭代次数。

I have initially taken an array of size 3 and I am providing 3 inputs with each input of different array size and then I iterate twice for finding the maximum product , If I don't find any prime number , I output -1 on the output screen . 我最初采用了一个3号数组,我提供3个输入,每个输入不同的数组大小,然后我迭代两次找到最大的产品,如果我没有找到任何素数,我在输出上输出-1屏幕。 Th first line depicts the total number of inputs which can be viewed as the number of test cases and for each test case , the first line depicts the size of array and the second line depicts the elements present in the array .Please help me to identify the problem , for the first input it is printing -1 but with some issues , Ideally , it should only go to if part 第一行描述了可以被视为测试用例数量的输入总数,对于每个测试用例,第一行描述了数组的大小,第二行描述了数组中存在的元素。请帮我识别问题,对于第一个输入它是打印-1但有一些问题,理想情况下,它应该只去部分

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

but it is going to code snippet of else part , 但它将编写其他部分的代码片段,

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

This I checked by writing printf statements in the else part , so I am able to get the value printed for the first input ,this is quite confusing since when if part is getting executed then why is the else part getting executed ?And for the rest inputs , it is printing garbage values . 我通过在else部分编写printf语句来检查,所以我能够获得为第一个输入打印的值,这是非常令人困惑的,因为如果部分正在执行那么为什么else部分被执行?其余部分输入,它是打印垃圾值。

Here is a input set: 这是一个输入集:

3

5
1 4 6 8 10

3
2 2 9

2
156 13 

The corresponding output is: 相应的输出是:

-1
4
169

You should scan array[j] instead of array[n] . 你应该扫描array[j]而不是array[n] nth index is not valid. 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;
    }
}

Previously, you had a far complex idea. 以前,你有一个非常复杂的想法。 Here is some optimizations done to your code. 以下是对代码进行的一些优化。 Feel free to ask anything. 随意问什么。 Hope it helps :) 希望能帮助到你 :)

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM