求大佬帮忙看看为什么这样也过不了

P1579 哥德巴赫猜想(升级版)

Willis_Wong @ 2019-09-28 21:17:52

#include<stdio.h>
#include<math.h>
int n,a[3],i,j,k,x,y,z;
void isprime(int c, int d);
int main()
{
    scanf("%d", &n);
    int max = n;
    if (n > 20000)
    {
        max = 20000;
    }
    for (i = 11; i < max; i += 2)
    {
        isprime(i, x);
        if(x == 1)
        {
            a[1] = i;
            for (j = 11; j < max; j += 2)
            {
                isprime(j, y);
                if (y == 1)
                {
                    a[2] = j;
                    for (k = 11; k < max; k += 2)
                    {
                        isprime(k,z);
                        if (z == 1)
                        {
                            a[3] = z;
                            if (a[1] + a[2] + a[3] == n)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    printf("%d %d %d", a[1], a[2], a[3]);
}
void isprime(int c, int d)
{
    int i;
    d = 0;
    for (i = 11; i < c; i++)
    {
        if (c % i == 0)
        {
            d = 1;
        }
    }
}

by ____OccDreamer @ 2019-09-28 21:28:27

@Willis_Wong 可以说下你的思路吗?


by Willis_Wong @ 2019-09-28 21:43:47

@火草yu 就直接定义一个判断素数的函数,如果是素数就显示值为1,否则为0,然后经过三轮判断都是素数后,再相加,如果等于n,直接跳出循环输出,否则继续


by ____OccDreamer @ 2019-09-29 05:38:18

@Willis_Wong 嗯......可以直接从2枚举到n,用两重循环就够了,比如:

for (int j=2;j<=n-4;j++)
        for (int p=2;p<=n-4;p++)

另一个用n-j-p表示即可


by Willis_Wong @ 2019-09-29 10:44:23

@火草yu 但我还是全红啊


by ____OccDreamer @ 2019-09-29 21:19:17

@Willis_Wong emmmmm......你改了别的地方吗?可以发一下你改了后的代码吗?


by Willis_Wong @ 2019-09-30 12:29:35

#include<stdio.h>
#include<math.h>
int n,a[3],i,j,k,x,y,z;
void isprime(int c, int d);
int main()
{
    scanf("%d", &n);
    int max = n;
    if (n > 20000)
    {
        max = 20000;
    }
    for (i = 11; i < max; i += 2)
    {
        isprime(i, x);
        if(x == 1)
        {
            a[1] = i;
            for (j = 11; j < max; j += 2)
            {
                isprime(j, y);
                if (y == 1)
                {
                    a[2] = j;
                    a[3] = n-i-j;
                    isprime(a[3],z);
                            if (z==1)
                            {
                                break;
                            }

                }
            }
        }
    }
    printf("%d %d %d", a[1], a[2], a[3]);
}
void isprime(int c, int d)
{
    int i;
    d = 0;
    for (i = 11; i < c; i++)
    {
        if (c % i == 0)
        {
            d = 1;
        }
    }
}

@火草yu


by ____OccDreamer @ 2019-09-30 20:42:55

@Willis_Wong 咳,就是数组是从0开始的,像你的a[3]实际没越界的只有a[0],a[1],a[2];还有,要从2开始循环,

for (i = 11; i < c; i++)
    {
        if (c % i == 0)
        {
            d = 1;
        }
    }

for (i = 11; i < max; i += 2)

for (j = 11; j < max; j += 2)

然后

for (i = 11; i < max; i += 2)

for (j = 11; j < max; j += 2)

到n-4就可以了,你改改试试。


by Monkey_Hunter @ 2019-11-09 08:53:31

return 0;


|