hdu 1016

#include 
using namespace std;
int prime[41] = { 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 };//mark the prime
int vis[21];
//int founded[21];
int line[21];//save the line
int n;
bool check(int pri) {//if pri is prime,return true
    return prime[pri];
}

void dfs(int past=1,int Cnt=1) {
    ++Cnt;
    if (Cnt == n+1&&prime[past+1]) {
        cout << 1;
        for (int i = 2; i <= n;++i)cout <<" "<< line[i];
        cout << endl;
        --Cnt;
        return;
    }
    for (int i = 2; i <= n; ++i) {
        if (vis[i])continue;
        if (check(i + past)) {
            vis[i] = 1;
            line[Cnt] = i;//save
            dfs(i, Cnt);
            vis[i] = 0;
        }
        else {
            continue;
        }
    }
    --Cnt;
    return;
}
int main() {
    int c = 0;
    while (cin >> n) {
        ++c;
        cout << "Case " << c << ":" << endl;
        dfs();
        cout << endl;
    }
}

你可能感兴趣的:(hdu,AC,code)