题目1459:Prime ring problem
时间限制:2 秒内存限制:128 兆特殊判题:否提交:826解决:334
题目描述:
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.
输入:
n (1 < n < 17).
输出:
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.
样例输入:
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
提示:
用printf打印输出。
【解题思路】
1、求解出17以内的所有素数。
2、针对从1开始,判断相邻的两个数是否为素数。
3、dfs求解打印结果,注意每两个case之间有一行空行。
时间限制:2 秒内存限制:128 兆特殊判题:否提交:826解决:334
题目描述:
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.
输入:
n (1 < n < 17).
输出:
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.
样例输入:
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
提示:
用printf打印输出。
【解题思路】
1、求解出17以内的所有素数。
2、针对从1开始,判断相邻的两个数是否为素数。
3、dfs求解打印结果,注意每两个case之间有一行空行。
Java AC
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.List;
public class Main {
/*
* 1459 2014年6月29日12:07:05
*/
private static int len;
private static List<Integer> primeList;
public static void main(String[] args) throws Exception {
len = 60;
initPrimeList();
int seq = 1;
StreamTokenizer st = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) st.nval;
System.out.println("Case "+seq+":");
int visit[] = new int[n+1];
int array[] = new int[n+1];
array[1] = 1;
dfs(array, visit, 2, n);
System.out.println();
seq++;
}
}
private static void dfs(int[] array, int[] visit, int start, int n) {
if (start == n+1) {
StringBuffer sb = new StringBuffer();
if (primeList.contains(1 + array[n])) {
for (int i = 1; i < n; i++) {
sb.append(array[i]+" ");
}
sb.append(array[n]);
System.out.println(sb);
}
return;
}
for (int i = 2; i < n+1; i++) {
if (visit[i] == 0 && primeList.contains(i + array[start - 1])) {
visit[i] = 1;
array[start] = i;
dfs( array, visit, start+1,n);
visit[i] = 0;
}
}
}
private static void initPrimeList() {
int array[] = new int[len];
for (int i = 1; i < len; i++) {
if (i % 2 == 0) {
array[i] = 0;
} else {
array[i] = 1;
}
}
array[1] = 0;
array[2] = 1;
for (int i = 3; i < len; i++) {
if (array[i] == 1) {
for (int j = 2 * i; j < len; j += i) {
array[j] = 0;
}
}
}
primeList = new ArrayList<Integer>();
for (int i = 1; i < len; i++) {
if (array[i] == 1) {
primeList.add(i);
}
}
}
}
/**************************************************************
Problem: 1459
User: wzqwsrf
Language: Java
Result: Accepted
Time:1590 ms
Memory:105052 kb
****************************************************************/
C++ AC
#include <stdio.h>
#include <string.h>
const int len = 60;
const int maxN = 20;
int array[len];
int primeArr[len];
int numArr[maxN];
int visit[maxN];
int n;
void initArray(){
int i, j;
for(i = 1; i < len; i++){
if(i % 2 == 0){
array[i] = 0;
}else{
array[i] = 1;
}
}
array[1] = 0;
array[2] = 1;
for(i = 3; i < len; i++){
if(array[i] == 1){
for(j = 2 * i; j < len; j += i){
array[j] = 0;
}
}
}
memset(primeArr, 0, sizeof(primeArr));
int k = 0;
for(i = 1; i < len; i++){
if(array[i] == 1){
primeArr[i] = 1;
}
}
}
void dfs(int start, int n){
int i, j;
if(start == n + 1){
if(primeArr[1+numArr[n]] == 1){
for(i = 1; i < n; i++){
printf("%d ", numArr[i]);
}
printf("%d\n", numArr[n]);
}
return;
}
for(i = 2; i < n+1; i++){
if(visit[i] == 0 && primeArr[i + numArr[start - 1]] == 1){
visit[i] = 1;
numArr[start] = i;
dfs(start+1, n);
visit[i] = 0;
}
}
}
int main(){
initArray();
int seq = 1;
while(scanf("%d", &n) != EOF){
printf("Case %d:\n", seq);
memset(numArr, 0, sizeof(numArr));
memset(visit, 0, sizeof(visit));
numArr[1] = 1;
dfs(2, n);
printf("\n");
seq++;
}
return 0;
}
/**************************************************************
Problem: 1459
User: wzqwsrf
Language: C++
Result: Accepted
Time:400 ms
Memory:1020 kb
****************************************************************/