首页 > 其他分享 >hdu-1016

hdu-1016

时间:2023-03-03 13:03:59浏览次数:56  
标签:hdu 20 int pos result 1016 include

约瑟夫换问题

http://acm.hdu.edu.cn/showproblem.php?pid=1016


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>

int n,cas=1,visit[20],result[20]={0,1}; 
int p[]={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};

void DFS(int pos)
{
    int i;
    if(pos==n && p[result[1]+result[n]])
    {
        printf("%d",result[1]);
        for(i=2;i<=n;i++)
            printf(" %d",result[i]);
        printf("\n");
        return;
    }

    for(i=2;i<=n;i++)
    {
        if(!visit[i] && p[i+result[pos]])
        {
            result[pos+1]=i;
            visit[i]=1;
            DFS(pos+1);
            visit[i]=0;
        }
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF )
    {
        memset(visit,0,sizeof(visit));
        printf("Case %d:\n",cas++);
        if(n%2==0)
            DFS(1);
        printf("\n");
    }
}



标签:hdu,20,int,pos,result,1016,include
From: https://blog.51cto.com/u_15990681/6098470

相关文章

  • hdu-1238
    http://acm.hdu.edu.cn/showproblem.php?pid=1238SubstringsTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalS......
  • hdu-1515
    dfs 题意:给你两个字符串,问:第一个字符串按入栈出栈规则,能否达到第二个字符串,输出所有的方法,i表示入栈,o表示出栈。用dfs模拟第一个字符串入栈出栈过程:1.当前字符......
  • hdu-1548
    搜索做着做着成最短路径了。。dij本层可以直接到达的层数距离为1否则为无穷大#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#includ......
  • hdu-1253
    http://acm.hdu.edu.cn/showproblem.php?pid=1253这道水题#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<......
  • hdu-2821
    http://acm.hdu.edu.cn/showproblem.php?pid=2821不要被题目吓到,认真读题还是好理解的#include<stdio.h>#include<iostream>#include<string.h>#include<math.h......
  • HDU-5112-A Curious Matt (2014ACM/ICPC北京赛区现场赛A题!)
    http://acm.hdu.edu.cn/showproblem.php?pid=5112排序之后计算就好开始用cin超时了#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#......
  • hdu-5122
    http://acm.hdu.edu.cn/showproblem.php?pid=5122简单题#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include......
  • HDUOJ 2041-2055
    2041超级楼梯ProblemDescription有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input输入数据首先包含一个整数N,表示测试实......
  • pat乙级1016 部分A+B
    #include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>intmain(){intcounta=0,countb=0;intpa=0,pb=0;chara[10],......
  • Problem C HDU - 5687
    现在有个字典要支持一下操作 1、insert:往神奇字典中插入一个单词  2、delete:在神奇字典中删除所有前缀等于给定字符串的单词  3、search:查询是否在神奇......