UVA524- Prime Ring Problem

题目链接

题意:给出整数n,输出所有素数环

思路:回溯求解,注意输出时,每两组之间输出一个空行

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 105;
int n;
int arr[MAXN], prime[MAXN], vis[MAXN];

int is_prime(int x) {
    if (x == 2) return 1;
    for (int i = 2; i < MAXN; i++)
        if (x != i && x % i == 0)
            return 0;
    return 1;
}

void init() {
    for (int i = 2; i < MAXN; i++)
        prime[i] = is_prime(i);
}

void dfs(int cur) {
    if (cur == n && prime[arr[0] + arr[n - 1]]) {
        printf("%d", arr[0]);
        for (int i = 1; i < n; i++)
            printf(" %d", arr[i]);
        printf("\n");
    }
    else {
        for (int i = 2; i <= n; i++)
            if (!vis[i] && prime[i + arr[cur - 1]]) {
                arr[cur] = i;
                vis[i] = 1;
                dfs(cur + 1);
                vis[i] = 0;
            }
    }
}

int main() {
    int t = 0;
    init();
    while (scanf("%d", &n) != EOF) {
        arr[0] = 1;
        memset(vis, 0, sizeof(vis));
        if (t)
            printf("\n");
        printf("Case %d:\n", ++t);
        dfs(1);
    }
    return 0;
}

题目链接题目链接

UVA524- Prime Ring Problem,布布扣,bubuko.com

时间: 2024-08-15 00:44:37

UVA524- Prime Ring Problem的相关文章

UVa524 Prime Ring Problem (回溯法)

链接:http://acm.hust.edu.cn/vjudge/problem/19667分析:先打个素数表,题目要求从1开始打印,直接把1固定到A[0]位置,打印的时候就可以直接从A[0]开始打印了,然后枚举2~n,vis判断之前是否用过,和是否为素数,都满足则继续递归枚举,然后回溯将vis还原. 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 int n, isp[35], A[20]

UVA524 素数环 Prime Ring Problem

题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-1016 zoj 1457  :https://vjudge.net/problem/ZOJ-1457 题意翻译 输入正整数n,把整数1,2,...,n组成一个环,使得相邻两个整数之和均为素数.输出时,从整数1开始逆时针排列.同一个环恰好输出一次.. 多组数据,读入到EOF结束. 第i组数据输出前加上一

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1

[ACM] 1016 Prime Ring Problem (深度优先搜索)

Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle

uva 524 prime ring problem——yhx

  Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always

HDU 1016 Prime Ring Problem(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32872    Accepted Submission(s): 14544 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

HDOJ-1016 Prime Ring Problem【DFS】

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47217    Accepted Submission(s): 20859 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /