HDU 1016 Prime Ring Problem

Posted lemon1234

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1016 Prime Ring Problem相关的知识,希望对你有一定的参考价值。

/*
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. 

 
Input
n (0 < n < 20). 
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order. 

You are to write a program that completes above process. 

Print a blank line after each case. 
Sample Input
6
8
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
*/

明明是同样的思想,C代码AC而JAVA代码却总是WA。。。。

呜呜呜┭┮﹏┭┮

 

#include<stdio.h>
#include<string.h>
int record[25],vis[25];
int prime[40]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1};
int n,cnt = 1;
void dfs(int step){
    if(step>n){
        if(prime[record[n]+1]==0){
            for(int i=1;i<=n;i++){
                if(i==1) printf("%d",record[i]);
                else printf(" %d",record[i]);
            }
            printf("\n");
        }
        
        return;
    }
    for(int i=2;i<=n;i++){
        if(prime[record[step-1]+i]||vis[i])  continue;
        record[step] = i;
        vis[i] = 1;
        dfs(step+1);
        vis[i] = 0;
    }
    
}
int main()
{
    while(~scanf("%d",&n)){
        memset(record,0,sizeof(record));
        memset(vis,0,sizeof(vis));
        record[1] = vis[1] = 1;
        if(n==1)  printf("Case %d:\n1\n\n",cnt++);
        else if(n%2==0){
            printf("Case %d:\n",cnt++);
            dfs(2);
            printf("\n");
        }
    }
    return 0;
}

 

 

import java.util.*;

public class Main {
    
    static int[] record;
    static boolean[] vis;
    static boolean[] check;
    static int prime[];
    static int n,t,cnt=1;
    
    
    static void LinePrime() {
        
        check[0] = check[1] =true;
        
        for(int i=2; i<100; i++) {
            if(!check[i])
                prime[t++] = i;
            for(int j=0; j<t;j++) {
                if(i*prime[j]>=100)  break;
                check[i*prime[j]] = true;
                if(i%prime[j]==0)  break;
            }
        }
    }
    static void  DFS(int num) {
        if(num >n) {
            if(!check[record[n]+1]) {
                for(int i=1;i<=n;i++)
                    System.out.print(record[i]+" ");
                System.out.println();
            }
            return;
        }
        for(int i=2; i<=n; i++) {
            if(check[i+record[num-1]]||vis[i]) continue;
            record[num] = i;
            vis[i] = true;
            DFS(num+1);
            vis[i] = false;
        }

    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        check = new boolean[100];
        prime = new int[100];
        record = new int[25];
        vis = new boolean[25];
        
        LinePrime();
        
        while(scan.hasNext()) {
            n = scan.nextInt();
            if(n==1) {
                System.out.printf("Case %d:\n",cnt++);
                System.out.println("1");
                
            }else if(n%2==0) {
                System.out.printf("Case %d:\n",cnt++);
                record[1] = 1;
                DFS(2);    
                
            }
            
            
        }

    }
}

 

以上是关于HDU 1016 Prime Ring Problem的主要内容,如果未能解决你的问题,请参考以下文章

hdu1016 Prime Ring Problem

HDU 1016 Prime Ring Problem(DFS)

HDU-1016-Prime Ring Problem

Prime Ring Problem HDU - 1016 (dfs)

Prime Ring Problem HDU - 1016

HDU 1016 Prime Ring Problem