蓝桥杯---剪邮票

剪邮票


如【图1】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2】,【图3】中,粉红色所示部分就是合格的剪取。



public class Main{



public static void main(String[] agrs) {


int a, b, c, d, e;
int temp = 0;
int[] sum = new int[5];
StringBuffer str = new StringBuffer();


for (a = 1; a <= 12; a++) {


for (b = 1; b <= 12; b++) {


if (b != a && ((Math.abs(b - a) == 1 && (a + b) != 9 && (a + b) != 17) || Math.abs(b - a) == 4)) {


for (c = 1; c <= 12; c++) {


if (c != b && c != a
&& (((Math.abs(c - b) == 1 && (c + b) != 9 && (c + b) != 17) || Math.abs(c - b) == 4)
|| ((Math.abs(c - a) == 1 && (c + a) != 9 && (c + a) != 17)
|| Math.abs(c - a) == 4))) {


for (d = 1; d <= 12; d++) {


if (d != a && d != c && d != b
&& (((Math.abs(d - c) == 1 && (c + d) != 9 && (c + d) != 17)
|| Math.abs(d - c) == 4)
|| ((Math.abs(d - b) == 1 && (d + b) != 9 && (d + b) != 17)
|| Math.abs(d - b) == 4)
|| ((Math.abs(d - a) == 1 && (d + a) != 9 && (d + a) != 17)
|| Math.abs(d - a) == 4))) {


for (e = 1; e <= 12; e++) {


if (e != a && e != c && e != b && e != d
&& (((Math.abs(e - d) == 1 && (e + d) != 9 && (e + d) != 17)
|| Math.abs(e - d) == 4)
|| ((Math.abs(e - c) == 1 && (e + c) != 9 && (e + c) != 17)
|| Math.abs(e - c) == 4)
|| ((Math.abs(e - b) == 1 && (e + b) != 9 && (e + b) != 17)
|| Math.abs(e - b) == 4)
|| ((Math.abs(e - a) == 1 && (e + a) != 9 && (e + a) != 17)
|| Math.abs(e - a) == 4))) {


sum[0] = a;
sum[1] = b;
sum[2] = c;
sum[3] = d;
sum[4] = e;


quickSort(sum, 0, 4);


String strOne = sum[0] + "" + sum[1] + "" + sum[2] + "" + sum[3] + ""
+ sum[4];


if (VerificationString(str, strOne)) {
temp++;


System.out.println(a + " " + b + " " + c + " " + d + " " + e);
}
}
}
}
}
}
}
}
}
}


System.out.print(temp);
}


public static boolean VerificationString(StringBuffer str, String strOne) {// 比较是否有该组合


String strTwo = str.toString();


String[] strThree = strTwo.split(" ");


if (strThree != null) {


for (int i = 0; i < strThree.length; i++) {


if (strThree[i].equals(strOne))
return false;
}
}


str.append(" ").append(strOne);


return true;
}


public static void quickSort(int[] a, int low, int high) {// 对这些数进行快速排序


int i = low, j = high;


int temp = a[i];


while (i < j) {


while (i < j && temp < a[j])
j--;


if (i < j) {


a[i] = a[j];
i++;
}


while (i < j && a[i] < temp)
i++;


if (i < j) {


a[j] = a[i];
j--;
}
}
a[i] = temp;


if (low < i)
quickSort(a, low, i - 1);
if (high > i)
quickSort(a, j + 1, high);
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值