Program C 暴力求解

Description

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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 <= 16))
-->

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.

You are to write a program that completes above process.

Sample Input

6
8

Sample Output

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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int is_prime(int n)
{
  for(int k= 2; k*k <= n; k++)
    if(n% k== 0) return 0;
  return 1;
}

int n, A[50], isp[50], vis[50];
void dfs(int cur)
{
  if(cur == n && isp[A[0]+A[n-1]])
    {
        for(int i = 0; i < n; i++)
        {
            if(i != 0)
                printf(" ");
            printf("%d", A[i]);
        }
        printf("\n");
  }
  else
    for(int i = 2; i <= n; i++)
        if(!vis[i] && isp[i+A[cur-1]])
        {
            A[cur] = i;
            vis[i] = 1;
            dfs(cur+1);
            vis[i] = 0;
        }
}

int main()
{
  int kase = 0;
  while(scanf("%d", &n) == 1 && n > 0)
    {
        if(kase > 0)
            printf("\n");
        printf("Case %d:\n", ++kase);
    for(int i = 2; i <= n*2; i++)
        isp[i] = is_prime(i);
    memset(vis, 0, sizeof(vis));
    A[0] = 1;
    dfs(1);
  }
  return 0;
}
				
时间: 2024-10-13 01:04:08

Program C 暴力求解的相关文章

Program A - 暴力求解

Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where 2<=N<=79. That is, abcde / fghi

Program L 暴力求解

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

Program B 暴力求解

Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot ?nd a positive sequence, you should consider 0 as the value of the maximum product

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)

/** * 最大子数组的暴力求解算法,复杂度为o(n2) * @param n * @return */ static MaxSubarray findMaxSubarraySlower(int[] n) { long tempSum = 0; int left = 0; int right = 0; long sum = Long.MIN_VALUE; for (int i = 0; i < n.length; i++) { for (int j = i; j < n.length; j++

求解最大子数组问题 -- 暴力求解 和 分治法求解

/*------------------ 求解最大子数组问题 --------------- 最大子数组,就是求解一个数组的所有元素的各种组合中,和最大的 那个子数组.在这种情况下,如果元素值全部非负,那么最大子数组当然 是所有元素.但是如果有负值的元素存在,那么久需要找到一个由数组中 连续几个元素组成的子数组并且其元素值之和最大. */ #include <iostream> using namespace std; struct ArrayStruct { ArrayStruct(int

BZOJ 1054题解 BFS暴力求解

BZOJ 1054题解 BFS暴力求解 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1884  Solved: 1033[Submit][Status][Discuss] Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移 动到某人

POJ 1562(L - 暴力求解、DFS)

油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerou

HDU 1431 素数回文【暴力求解】

/* 题目大意:找一个范围内的所有素数回文数 解题思路:打一个表将1亿以内所有的素数回文数找出来,大概有780个这样子 关键点:暴力求解 解题人:lingnichong 解题时间:2014-08-29 12:02:55 解题体会:如果按一般方法打个素数表,很容易超内存(MLE),所以就先将所有的素数回文全部算出来,再在这个数组里面找在题上那个范围的所有素数回文数 */ 素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655