[Java] 纯文本查看 复制代码
public class Practice {
public static void main(String[] args) {
int count = 1;
for(int g = 1;g <= 4 ; g++) {
for(int s = 1; s <= 4; s++) {
for(int b = 1; b < 5; b++) {
for(int q = 1; q < 5 ; q++) {
if(g != s && g != b && g != q && s != b && s != q && b != q){
// 需求2
// if(q != 4) {
// if((q + b) != 4 && (b + s) != 4 && (s + g) != 4)
System.out.println(count++ + ":" + (1000 * q + 100 * b + 10 * s + g));
// }
}
}
}
}
}
}
}
[Java] 纯文本查看 复制代码
public static void main(String[] args) {
// TODO Auto-generated method stub
int g = 0;
int s = 0;
int b = 0;
int q = 0;
int count = 0;
for (int i = 0; i <= 4321; i++) {
g = i % 10;
s = i % 100 / 10;
b = i % 1000 / 100;
q = i / 1000;
if (g < 5 && g > 0 && s < 5 && s > 0 && b < 5 && b > 0 && q < 5 && q > 0) {
if (g != s && g != b && g != q && s != b && s != q && b != q) {
// if (q != 4 && g + s != 4 && s + b != 4 && b + q != 4) {
count++;
System.out.print(i + " ");
// }
}
}
}
System.out.println("一共" + count);
}
[Java] 纯文本查看 复制代码
class Test {
public static void main(String[] args){
int cnt2 = fun01("", "1234");
System.out.println("cnt: "+cnt2);
int cnt3 = fun02("", "1234");
System.out.println("cnt: "+cnt3);
}
public static int fun01(String res, String source){
if ( res.length() >= source.length()) { //保证长度
//System.out.println(res);
return 1;
}
int cnt = 0;
for ( char c : source.toCharArray() ) {
if ( res.contains( c+"" ) ) { //保证不重复
continue;
}
cnt += fun01(res+c, source);
}
return cnt;
}
public static int fun02(String res, String source){
if ( res.length() >= source.length()) { //保证长度
//System.out.println(res);
return 1;
}
int cnt = 0;
for ( char c : source.toCharArray() ) {
if ( res.contains( c+"" ) ) { //保证不重复
continue;
}
if ( "".equals(res) && c == '4' ) { //4不能开头
continue;
}
if ( (res.endsWith("3") && c=='1') || (res.endsWith("1") && c=='3') ) { //1,3不在一起
continue;
}
cnt += fun02(res+c, source);
}
return cnt;
}
}