• 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

    InputcopyOutputcopy
     
    6 8 
     
    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

    中文题意:

    如图所示,一个环由 n 个圆组成。将自然数 1, 2, ..., n 分别放入每个圆圈中,相邻两个圆圈中的数字之和应为素数。

    注意:第一个圆圈的数量应始终为 1。

     

    输入

    n (0 < n < 20)。

    输出

    输出格式如下面的示例所示。每行代表圆环中从 1 开始顺时针和逆时针方向的一系列圆圈数字。数字的顺序必须满足上述要求。按字典顺序打印解决方案。

    您将编写一个完成上述过程的程序。

    在每个案例之后打印一个空行。

     

     AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. int num[21];
    11. int vis[21];//标记是否被使用
    12. int n;
    13. bool prime(int x){//使用六素数法判断素数
    14. if(x<=1)return false;
    15. if(x==2||x==3||x==5)return true;
    16. if(x%2==0||x%3==0)return false;
    17. for(int i=5;i<=sqrt(x);i+=5){
    18. if(x%i==0||x%(i=2)==0)return false;
    19. }
    20. return true;
    21. }
    22. void dfs(int cnt){
    23. if(cnt==n&&prime(1+num[n-1])){//满足条件就输出
    24. for(int i=0;i
    25. if(i!=n-1)cout<" ";
    26. else cout<
    27. }
    28. vis[num[n-1]]=0;//用完最后一个数也将其置位0
    29. }
    30. if(cnt==n)return;
    31. for(int i=2;i<=n;i++){
    32. if (vis[i]==0&&prime(num[cnt-1]+i)){//满足下一次搜索条件
    33. vis[i]=1;
    34. num[cnt]=i;
    35. dfs(cnt+1);//搜索下一次
    36. vis[i]=0;//切记归位
    37. }
    38. }
    39. }
    40. int main(){
    41. ios::sync_with_stdio(false);
    42. cin.tie(0),cout.tie(0);
    43. int T=1;
    44. while(cin>>n){
    45. memset(vis,0,10);
    46. num[0]=1;//确保每一次的第一个都是1
    47. vis[1]=1;//标记1已经使用了
    48. cout<<"Case "<":"<
    49. dfs(1);
    50. cout<
    51. }
    52. }

     使用stl库中的next_permutation函数但TLE代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. int a[22];
    11. bool solve(int x,int y){
    12. if(x+y==2||x+y==5||x+y==3)return false;
    13. if((x+y)%2==0||(x+y)%3==0)return true;
    14. for(int i=5;i<=sqrt(x+y);i+=5){
    15. if((x+y)%i==0||(x+y)%(i+2)==0)return true;
    16. }
    17. return false;
    18. }
    19. int main(){
    20. int n;
    21. int T=1;
    22. while(cin>>n){
    23. for(int i=0;i1;
    24. int cnt=1;
    25. do{
    26. bool flag=true;
    27. for(int i=1;i
    28. if(solve(a[i],a[i-1])){
    29. flag=false;
    30. break;
    31. }
    32. }
    33. if(solve(a[0],a[n-1]))flag=false;
    34. if(flag){
    35. if(cnt==1)cout<<"Case "<":"<
    36. cnt=2;
    37. for(int i=0;i
    38. if(i!=n-1)cout<" ";
    39. else cout<
    40. }
    41. }
    42. }while(next_permutation(a+1,a+n));
    43. T++;
    44. cout<
    45. }
    46. return 0;
    47. }

  • 相关阅读:
    双冠王!华为云领跑政务市场
    Taurus.MVC WebAPI 入门开发教程8:WebAPI文档与自动化测试。
    java毕业设计——基于Java+Javamail的邮件收发系统设计与实现(毕业论文+程序源码)——邮件收发系统
    Docker 容器编排
    【第三章】神经网络的架构-前馈神经网络
    java毕业设计财务信息管理mybatis+源码+调试部署+系统+数据库+lw
    基于springboot的家政系统 毕业设计-附源码201524
    MutationObserver对象
    【BUG 弹药库】二分模板的优化
    【场景化解决方案】构建门店通讯录,“门店通”实现零售门店标准化运营
  • 原文地址:https://blog.csdn.net/qq_51580852/article/details/126087622