您的位置:首页 > 理论基础

标题:五星填数 如【图1.png】的五星图案节点填上数字:1~12,除去7和11。 要求每条直线上数字和相等。 如图就是恰当的填法。 请你利用计算机搜索所有可能的填法有多少种。 注意:旋转或镜

2018-02-07 11:08 330 查看
标题:五星填数

如【图1.png】的五星图案节点填上数字:1~12,除去7和11。

要求每条直线上数字和相等。

如图就是恰当的填法。

请你利用计算机搜索所有可能的填法有多少种。

注意:旋转或镜像后相同的算同一种填法。

请提交表示方案数目的整数,不要填写任何其它内容。

答案12

package com.company;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Mr.Smart on 2018-02-07.
*/
public class Main18 {
private static int count = 0;
private static List<Integer> source;//定义一个集合用来初始化1-12数字,除去7和11
private static List<Integer> result;
private static List<Integer> tsource;
private static List<Integer> tresult;
public static void main(String[] args){
source = new ArrayList<>();
result = new ArrayList<>();
for (int i=1;i<=12;i++){
source.add(i);//初始化sorce集合
}
source.remove(6);//删除7
source.remove(9);//删除11因为删除7之后位置下标会减一所以下标为9
dfs(source,result);//调用dfs函数
System.out.println(count/10);//由于每种组合经过旋转和镜像之后都有10种不同的组合满足条件
}

private static void dfs(List<Integer> source, List<Integer> result) {

if (source.size()==0){//判断source是否为空,如果为空则说明source完成了一次排序
if (check()) count++;
}

for (int i=0;i<source.size();i++){
tsource = new ArrayList<>(source);//将source中的值赋给tsource
tresult = new ArrayList<>(result);//tresult
tresult.add(tsource.get(i));//向tresult中加入一个元素
tsource
4000
.remove(i);//并在tsource中删除该元素
dfs(tsource,tresult);
}
}
public static boolean check() //检验是不是相等
{
int a = tresult.get(0);
int b = tresult.get(1);
int c = tresult.get(2);
int d = tresult.get(3);
int e = tresult.get(4);
int f = tresult.get(5);
int g = tresult.get(6);
int h = tresult.get(7);
int i = tresult.get(8);
int j = tresult.get(9);
if (a+c+f+i == a+d+g+j && a+d+g+j == b+c+d+e && b+c+d+e == b+f+h+j&&b+f+h+j == e+g+h+i){
return true;
}
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐