欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hdu 1016 Prime Ring Problem

程序员文章站 2022-05-21 08:44:24
...
简单的搜索题,用递归


/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
Copyright (c) 2011 panyanyany All rights reserved.

URL : http://acm.hdu.edu.cn/showproblem.php?pid=1016
Name : 1016 Prime Ring Problem

Date : Wednesday, August 10, 2011
Time Stage : 2 hours less

Result:
4378651 2011-08-10 16:13:31 Accepted 1016
171MS 204K 1390 B
C++ pyy


Test Data:

Review:
//----------------------------------------------------------------------------*/

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

#define MAXSIZE 40

int n, len, tcase ;
int prime[MAXSIZE], ans[MAXSIZE], used[MAXSIZE] ;

void dfs (int a)
{
ans[len++] = a ;
used[a] = 1 ;

int i ;
if (len - 1 == n)
{
if (prime[ans[1]+a])
{
for (i = 1 ; i < n ; ++i)
{
printf ("%d ", ans[i]) ;
}
printf ("%d\n", ans[i]) ;
}
}
else
{
for (i = 2 ; i <= n ; ++i)
{
if (prime[i + a] && !used[i])
{
dfs (i) ;
}
}
}
--len ;
used[a] = 0 ;
}

int main ()
{
int i, j ;
memset (prime, 1, sizeof (prime)) ;
for (i = 2 ; i < MAXSIZE ; ++i)
{
if (prime[i])
for (j = i ; j*i < MAXSIZE ; ++j)
prime[j*i] = 0 ;
}

tcase = 0 ;
while (scanf ("%d", &n) != EOF)
{
memset (used, 0, sizeof (used)) ;
len = 1 ;
++tcase ;

printf ("Case %d:\n", tcase) ;
dfs (1) ;
puts ("") ;
}
return 0 ;
}