题解-A ring is compose of n circles as shown in diagram.
2018-03-25 21:35
387 查看
[align=left]Problem Description[/align]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.<br><br>Note: the number of first circle should always be 1.<br><br><img src=../../data/images/1016-1.gif><br>
[align=left]Input[/align]n (0 < n < 20).<br>
[align=left]Output[/align]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.<br><br>You are to write a program that completes above process.<br><br>Print a blank line after each case.<br>
[align=left]Sample Input[/align]68
[align=left]Sample Output[/align]Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
题目大意:
给你一个数n,输出所有的相邻两数之和为素数(首尾亦满足该条件)的1-n的排列,只输出以1开头的排列
解题思路:
刚看见题时闪过一丝偷懒的惊喜,前几天发现的那个函数next_permutation,正好全排列,我只需要判断一下是条件是否成立就行了,然而并不是所有的STL都是高效的,,试了好几次,默默加了个计数器,当输入为15时,基本上就不行了,,果然,函数的时间复杂度是n!,,数字一大,,准保准的超时,,老老实实写深搜,及时回溯,然后就过了
AC代码(with一段不舍得扔掉的大bug)#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100100],vis[100100];
int n;
int prim(int x)//x是否为素数
{
for(int i=2;i*i<=x;i++)
if(x%i==0)
return 0;
return 1;
}
void dfs(int c)//从1开始搜索
{
if(c==n&&prim(num[n-1]+1))//如果排到了最后一位且与第一位的和满足条件
{
printf("1");//保证第一位是1;
for(int i=1;i<n;i++)//实际从num[]的第二位输出
printf(" %d",num[i]);//一直到n-1位
printf("\n");
}
if(c==n) return ;
for(int i=2;i<=n;i++)
{
if(!vis[i]&&prim(num[c-1]+i))//满足与上一位和为素数,且数字没有被使用过
{
num[c]=i;//放入这个满足条件的数
vis[i]=1;//标记
dfs(c+1);//搜索下一位
vis[i]=0;
}
}
}
int main()
{
int k=1;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));//每一次的n,都要讲标记数组清零
num[0]=1;
vis[1]=1;//标记1被使用
printf("Case %d:\n",k++);
dfs(1);
printf("\n");
}
return 0;
}
//big bug..如下
//#include<iostream>
//#include<stdio.h>
//#include<algorithm>
//#include<math.h>
//using namespace std;
//int mark[25],a[25],n;
//int t=0;
//int su(int a[])
//{
// a[n+1]=a[1];
// for(int j=1;j<=n;j++)
// {
// int x=a[j]+a[j+1];
// for(int i=2;i<=sqrt(x);i++)
// {
// if(x%i==0)
// return -1;
// }
// }
// return 1;
//}
//void soso(int n)
//{
// do
// {
// t++;
// if(a[1]>1){break;return ;}
// if(su(a)>0&&(a[1]==1))
// {
// for(int j=1;j<=n;j++)
// cout<<a[j]<<" ";
// cout<<endl;
// }
// } while(next_permutation(a+1,a+n+1));//&&(a[1]==1)); // 1- 传入地址 2- 参数3是指参与排列的长度
// return ; //如果存在a之后的排列,就返回true;如果a是最后一个排列没
4000
有后记,返回false;每执行一次,a就变成它的后继
//}
//int main()
//{
// //int n;
// while(scanf("%d",&n)!=EOF&&(n<20))
// {
// for(int i=1;i<=20;i++)
// {
// a[i]=i;
// }
// //sizeof(mark,0,sizeof(mark));
// soso(n);
// }
// return 0;
//}
[align=left]Input[/align]n (0 < n < 20).<br>
[align=left]Output[/align]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.<br><br>You are to write a program that completes above process.<br><br>Print a blank line after each case.<br>
[align=left]Sample Input[/align]68
[align=left]Sample Output[/align]Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
题目大意:
给你一个数n,输出所有的相邻两数之和为素数(首尾亦满足该条件)的1-n的排列,只输出以1开头的排列
解题思路:
刚看见题时闪过一丝偷懒的惊喜,前几天发现的那个函数next_permutation,正好全排列,我只需要判断一下是条件是否成立就行了,然而并不是所有的STL都是高效的,,试了好几次,默默加了个计数器,当输入为15时,基本上就不行了,,果然,函数的时间复杂度是n!,,数字一大,,准保准的超时,,老老实实写深搜,及时回溯,然后就过了
AC代码(with一段不舍得扔掉的大bug)#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100100],vis[100100];
int n;
int prim(int x)//x是否为素数
{
for(int i=2;i*i<=x;i++)
if(x%i==0)
return 0;
return 1;
}
void dfs(int c)//从1开始搜索
{
if(c==n&&prim(num[n-1]+1))//如果排到了最后一位且与第一位的和满足条件
{
printf("1");//保证第一位是1;
for(int i=1;i<n;i++)//实际从num[]的第二位输出
printf(" %d",num[i]);//一直到n-1位
printf("\n");
}
if(c==n) return ;
for(int i=2;i<=n;i++)
{
if(!vis[i]&&prim(num[c-1]+i))//满足与上一位和为素数,且数字没有被使用过
{
num[c]=i;//放入这个满足条件的数
vis[i]=1;//标记
dfs(c+1);//搜索下一位
vis[i]=0;
}
}
}
int main()
{
int k=1;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));//每一次的n,都要讲标记数组清零
num[0]=1;
vis[1]=1;//标记1被使用
printf("Case %d:\n",k++);
dfs(1);
printf("\n");
}
return 0;
}
//big bug..如下
//#include<iostream>
//#include<stdio.h>
//#include<algorithm>
//#include<math.h>
//using namespace std;
//int mark[25],a[25],n;
//int t=0;
//int su(int a[])
//{
// a[n+1]=a[1];
// for(int j=1;j<=n;j++)
// {
// int x=a[j]+a[j+1];
// for(int i=2;i<=sqrt(x);i++)
// {
// if(x%i==0)
// return -1;
// }
// }
// return 1;
//}
//void soso(int n)
//{
// do
// {
// t++;
// if(a[1]>1){break;return ;}
// if(su(a)>0&&(a[1]==1))
// {
// for(int j=1;j<=n;j++)
// cout<<a[j]<<" ";
// cout<<endl;
// }
// } while(next_permutation(a+1,a+n+1));//&&(a[1]==1)); // 1- 传入地址 2- 参数3是指参与排列的长度
// return ; //如果存在a之后的排列,就返回true;如果a是最后一个排列没
4000
有后记,返回false;每执行一次,a就变成它的后继
//}
//int main()
//{
// //int n;
// while(scanf("%d",&n)!=EOF&&(n<20))
// {
// for(int i=1;i<=20;i++)
// {
// a[i]=i;
// }
// //sizeof(mark,0,sizeof(mark));
// soso(n);
// }
// return 0;
//}
相关文章推荐
- Flash.ActionScript3.殿堂之路 笔记 2 [ActionScript3 typeof is as in delete]
- Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
- Flex之旅:第一部分:flex必备基础知识积累(3)---is as typeof in instanceof 用法介绍
- as3数据类型检查写法(is/as/typeof/in/instanceof)用法介绍
- The Excel Connection Manager is not supported in the 64-bit version of SSIS, as no OLE DB provider i
- csharp: Use of Is and As operators in csharp
- Server(Admin) start is failing in WLST as outofmemory error permgen space for weblogic 12c
- The Excel Connection Manager is not supported in the 64-bit version of SSIS, as no OLE DB provider i 推荐
- this style of dress is in fashion
- What is the use of having destructor as private?
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated..
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- This generally means that another instance of this process was already running or is hung in the deb
- Warning[Pa082]: undefined behavior: the order of volatile accesses is undefined in this statement
- Assigning the return value of new by reference is deprecated in xxxx
- This version of the rendering library is more recent than your version of ADT plug-in. Please update
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- Flex的四种判断对象类型的方式 as instanceof is typeof比较
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- mysql5.7.12/13在安装新实例时报错:InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero