azazinco 2019-10-26 19:05 采纳率: 0%
浏览 1344
已结题

素数环:从1到20这20个数摆成一个环,要求相邻的两个环的和是一个素数。

为什么这段代码输出的是0?

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdlib>
using namespace std;
bool b[21] = {0};
int total = 0, a[21] = {0};
int search(int);
int print();
bool pd(int, int);
int main()
{
    search(1);
    cout << total << endl;
}
int search(int t)
{
    int i;
    for (i = 1; i <= 20; i++)
        if (pd(a[i - 1], i) && (!b[i])) {
            a[t] = i;
            b[i] = 1;
            if (t == 20) {
                if (pd(a[20], a[1]))
                    print();
            }
            else search(t + 1);
            b[i] = 0;
            }
}
int print()
{   
    total++;
    cout << "<" << total << ">";
    for (int j = 1; j <= 20; j++)
        cout << a[j] << " ";
    cout << endl;
}
bool pd(int x, int y)
{
    int k = 2, i = x + y;
    while (k <= sqrt(i) && i % k != 0)
        k++;
    if (k > sqrt(i))
        return 1;
    else return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-10-26 22:17
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料