工程课 蓝桥杯真题. 扑克序列
原题链接
简单
作者:
疯子orz
,
2024-03-30 19:03:35
,
所有人可见
,
阅读 4
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
//全排列
public class Main {
static char[] ways = new char[10];
static boolean[] st = new boolean[10];
static char[] c ;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
String s = "223344AA";
c = s.toCharArray();
dfs(0);
scan.close();
}
public static void dfs(int u) {
// TODO Auto-generated method stub
if(u == 8) {
int x1 = check('2');
int x2 = check('3');
int x3 = check('4');
int x4 = check('A');
if(x1 == 3 && x2 == 4 && x3 == 5 && x4 == 2) {
System.out.println(new String(ways));
System.exit(0);
}
return;
}
for(int i = 0;i < 8;i++) {
if(!st[i]) {
ways[u] = c[i];
st[i] = true;
dfs(u + 1);
st[i] = false;
}
}
}
private static int check(char target) {
// TODO Auto-generated method stub
// for(int i = 0;i < 8;i++) {
// if(ways[i] == target) {
// a1 = i;
// break;
// }
// }
// for(int i = 8;i >0 ;i--) {
// if(ways[i] == target) {
// a2 = i;
// break;
// }
// }
String s = new String(ways);
return s.lastIndexOf(target) - s.indexOf(target);//这两句等同于前面两个for循环
}
}