纸牌三角形
2018-03-30 14:53
176 查看
标题:纸牌三角形
A,2,3,4,5,6,7,8,9 共9张纸牌排成一个正三角形(A按1计算)。要求每个边的和相等。
下图就是一种排法(如有对齐问题,参看p1.png)。
A
9 6
4 8
3 7 5 2
这样的排法可能会有很多。
如果考虑旋转、镜像后相同的算同一种,一共有多少种不同的排法呢?
请你计算并提交该数字。
注意:需要提交的是一个整数,不要提交任何多余内容。
【解法一】
暴力循环package com.sise.test;
public class Test01 {
public static void main(String[] args){
int count=0;
for(int a=1;a<10;a++){
for(int b=1;b<10;b++){
if(a!=b){
for(int c=1;c<10;c++){
if(a!=c&&b!=c){
for(int d=1;d<10;d++){
if(a!=d&&b!=d&&c!=d){
for(int e=1;e<10;e++){
if(a!=e&&b!=e&&c!=e&&d!=e){
for(int f=1;f<10;f++){
if(a!=f&&b!=f&&c!=f&&d!=f&&e!=f){
for(int g=1;g<10;g++){
if(a!=g&&b!=g&&c!=g&&d!=g&&e!=g&&f!=g){
for(int h=1;h<10;h++){
if(a!=h&&b!=h&&c!=h&&d!=h&&e!=h&&f!=h&&g!=h){
for(int i=1;i<10;i++){
if(a!=i&&b!=i&&c!=i&&d!=i&&e!=i&&f!=i&&g!=i&&h!=i){
if((a+b+c+d==d+e+f+g)&&(a+b+c+d==g+h+i+a)){
count++;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
System.out.println(count/6);
}
}
【解法二】
全排列package com.sise.test;
public class Test01 {
public static int count=0;
public static void main(String[] args){
int[] str={1,2,3,4,5,6,7,8,9};
arrange(str,0);
System.out.println(count/6);
}
public static void arrange(int[] str,int st){
if(st>=str.length){
if(check(str)){
count++;
return;
}
}else{
for(int i=st;i<str.length;i++){
swap(str,st,i);
arrange(str,st+1);
swap(str,i,st);
}
}
}
public static boolean check(int[] a){
int[] b=new int[3];
b[0]=a[0]+a[1]+a[2]+a[3];
b[1]=a[3]+a[4]+a[5]+a[6];
b[2]=a[6]+a[7]+a[8]+a[0];
if(b[0]==b[1]&&b[0]==b[2]){
return true;
}
return false;
}
public static void swap(int[] str,int i,int j){
int temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
A,2,3,4,5,6,7,8,9 共9张纸牌排成一个正三角形(A按1计算)。要求每个边的和相等。
下图就是一种排法(如有对齐问题,参看p1.png)。
A
9 6
4 8
3 7 5 2
这样的排法可能会有很多。
如果考虑旋转、镜像后相同的算同一种,一共有多少种不同的排法呢?
请你计算并提交该数字。
注意:需要提交的是一个整数,不要提交任何多余内容。
【解法一】
暴力循环package com.sise.test;
public class Test01 {
public static void main(String[] args){
int count=0;
for(int a=1;a<10;a++){
for(int b=1;b<10;b++){
if(a!=b){
for(int c=1;c<10;c++){
if(a!=c&&b!=c){
for(int d=1;d<10;d++){
if(a!=d&&b!=d&&c!=d){
for(int e=1;e<10;e++){
if(a!=e&&b!=e&&c!=e&&d!=e){
for(int f=1;f<10;f++){
if(a!=f&&b!=f&&c!=f&&d!=f&&e!=f){
for(int g=1;g<10;g++){
if(a!=g&&b!=g&&c!=g&&d!=g&&e!=g&&f!=g){
for(int h=1;h<10;h++){
if(a!=h&&b!=h&&c!=h&&d!=h&&e!=h&&f!=h&&g!=h){
for(int i=1;i<10;i++){
if(a!=i&&b!=i&&c!=i&&d!=i&&e!=i&&f!=i&&g!=i&&h!=i){
if((a+b+c+d==d+e+f+g)&&(a+b+c+d==g+h+i+a)){
count++;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
System.out.println(count/6);
}
}
【解法二】
全排列package com.sise.test;
public class Test01 {
public static int count=0;
public static void main(String[] args){
int[] str={1,2,3,4,5,6,7,8,9};
arrange(str,0);
System.out.println(count/6);
}
public static void arrange(int[] str,int st){
if(st>=str.length){
if(check(str)){
count++;
return;
}
}else{
for(int i=st;i<str.length;i++){
swap(str,st,i);
arrange(str,st+1);
swap(str,i,st);
}
}
}
public static boolean check(int[] a){
int[] b=new int[3];
b[0]=a[0]+a[1]+a[2]+a[3];
b[1]=a[3]+a[4]+a[5]+a[6];
b[2]=a[6]+a[7]+a[8]+a[0];
if(b[0]==b[1]&&b[0]==b[2]){
return true;
}
return false;
}
public static void swap(int[] str,int i,int j){
int temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
相关文章推荐
- 标题:纸牌三角形
- 第八届蓝桥杯B组(Java) 第二题 纸牌三角形
- 第八届蓝桥杯 java B组 纸牌三角形
- 第八届蓝桥杯JavaB组-纸牌三角形
- 2017第八届蓝桥杯 03 纸牌三角形(java)
- 蓝桥杯2017第二题 纸牌三角形
- 纸牌三角形
- 蓝桥杯の纸牌三角形
- 第八届蓝桥杯纸牌三角形
- 第八届省赛 —— 题2 — 纸牌三角形
- 纸牌三角形and九宫幻方一些对称数组图形关于旋转和镜像的一些规律总结
- 蓝桥杯 纸牌三角形
- 第八届蓝桥杯 省赛B组第二题纸牌三角形
- 蓝桥杯之纸牌三角形
- 纸牌三角形问题
- 第八届蓝桥杯B组java省赛 纸牌三角形
- 蓝桥杯java第八届第二题--纸牌三角形
- 第四周项目一三角形类的构造函数(2)
- hdu 1249 三角形
- 第四周项目1三角形类的构造函数(3)