Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- bool flag[100];
- int store[100],n;
- bool prime(int n1)
- {
- if(n1<2)
- return 0;
- for(int i=2;i*i<=n1;i++)
- {
- if(n1%i==0)
- {
- return 0;
- }
- }
- return 1;
- }
- void print()
- {
- for(int i=1;i<n;i++)
- {
- cout<<store[i]<<" ";
- }
- cout<<store[n]<<endl;
- }
- void ringpro(int pos)
- {
- if(pos>n)
- {
- if(prime(store[1]+store[n]))
- print();
- return;
- }
- for(int i=2;i<=n;i++)
- {
- if(!flag[i] && prime(store[pos-1]+i))
- {
- store[pos]=i;
- flag[i]=1;
- ringpro(pos+1);
- flag[i]=0;
- }
- }
- }
- int main()
- {
- freopen("in.txt","r",stdin);
- freopen("out.txt","w",stdout);
- int cas=1;
- while(cin>>n)
- {
- if(cas>1)
- cout<<endl;
- cout<<"Case "<<cas++<<":"<<endl;
- memset(flag,0,sizeof(flag));
- store[1]=1;
- ringpro(2);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement