zoukankan      html  css  js  c++  java
  • 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem

    /*
        Name: NYOJ--488--素数环
        Author: shen_渊 
        Date: 15/04/17 15:30
        Description: DFS,素数打个表,37以内就够用了
    */
    
    #include<cstring>
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    void dfs(int);
    int n;
    //int prime[25] = {2,3,5,7,11,13,17,19,23,29,31,37};学到下面一招 
    bool prime[45]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0};
    int arr[22];
    int vis[22];
    int main() 
    {
    //    freopen("in.txt","r",stdin);
        int m = 0;
        while(cin>>n,n){
            memset(arr,0,sizeof(arr));
            memset(vis,0,sizeof(vis));
            arr[1] = 1;
            cout<<"Case "<<++m<<":
    ";
            if(n == 1)cout<<"1
    ";
            else if(n%2){
                cout<<"No Answer
    ";
            }else{
                dfs(2);
            }
        }
        return 0;
    }
    void dfs(int ct){
        if(ct == n+1 && prime[arr[n]+arr[1]]){
                for(int i=1; i<=n; ++i)
                    cout<<arr[i]<<" ";
                cout<<endl;
        }else{
            for(int i=2; i<=n; ++i){
                if(!vis[i] && prime[arr[ct-1]+i]){
                    arr[ct] = i;
                    vis[i] = 1;
                    dfs(ct+1);
                    vis[i] = 0;
                }
            }
        }
    }

     因为用ios::sync_with_stdio(false);取消了C的stream和C++的stream的同步,所以输出出现了不符合预期的结果,以后还是少用cin,cout,scanf,printf,C和C++混合输出的方式

    /*
        Name:hdu-1016-Prime Ring Problem 
        Copyright:
        Author:
        Date: 2018/5/19 16:59:52
        Description:水题 
    */
    #include <cstring>
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int n, arr[25], vis[25];
    int prime[40]={0,1,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0};//素数打表,因为n最大是20,所以只要打到40
    bool jundge(int n) {
        for (int i=2; i*i<=n; i++) {
            if (n%i == 0) return 0;
        }
        return 1;
    }
    void dfs(int ct) {
        if (ct == n) {
            if (prime[arr[ct] + 1]) {
                cout<<arr[1];
                for (int i=2; i<=n; i++) {
                    cout<<" "<<arr[i];
                }
                cout<<endl;
            }
        } else {
            for (int i=2; i<=n; i++) {
                if (vis[i] == 0 && prime[i+arr[ct]]) {
                    arr[ct+1] = i;
                    vis[i] = 1;
                    dfs(ct+1);
                    vis[i] = 0;
                }
            }
        }
    }
    int main()
    {
    //    ios::sync_with_stdio(false);
    //    加上这一句,用printf输出WA,cout输出AC,测试后发现,用文件读取输入输出的时候,
    //    printf并不能输出 Case 1:这一行的输出
        int k = 0;
        while (cin>>n) {
    //        printf("Case %d:
    ", ++k);
            cout<<"Case "<<++k<<":
    ";
            if (n == 1) {
                cout<<"1
    
    ";
                continue;
            }
            if (n & 1) {
                cout<<endl;
                continue;
            }
            memset(arr, 0, sizeof(arr));
            memset(vis, 0, sizeof(vis));
            arr[1] = 1;
            dfs(1);
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    自己编译GCC(compile gcc from source)
    sphinx PDF 中文
    rst2pdf 中文
    Pandoc PDF 中文
    Linux XMind
    asp.net 邮件发送类
    asp.net 音乐播放器
    使用X-UA-Compatible来设置IE浏览器兼容模式
    asp.net(c#)网页跳转七种方法小结
    C#中HashTable的用法
  • 原文地址:https://www.cnblogs.com/slothrbk/p/7251885.html
Copyright © 2011-2022 走看看