zoukankan      html  css  js  c++  java
  • 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正确,JAVA-Wrong Aswer中....

    #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("
    ");
            }
            
            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:
    1
    
    ",cnt++);
            else if(n%2==0){
                printf("Case %d:
    ",cnt++);
                dfs(2);
                printf("
    ");
            }
        }
        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:
    ",cnt++);
                    System.out.println("1");
                    
                }else if(n%2==0) {
                    System.out.printf("Case %d:
    ",cnt++);
                    record[1] = 1;
                    DFS(2);    
                    
                }
                
                
            }
    
        }
    }
  • 相关阅读:
    考在职还是全日制?
    ARP欺骗病毒攻击
    AutoCAD2006安装错误之解决
    WINCE下创建多个文件分区
    在WINCE5.0中应用CMD(比如运行PING命令)
    WinCE 应用程序开机自动方法
    Wince 添加中文字库
    WinCE内核裁减(中文字体)及字库和内核的分离(转)
    WINCE基于CH7024实现TV OUT (VGA)功能
    WINCE6.0 中文支持
  • 原文地址:https://www.cnblogs.com/Lemon1234/p/10609875.html
Copyright © 2011-2022 走看看