li--yong 发表于 2016-8-24 23:06
你确定不是数学题?
水月灬清影 发表于 2016-8-24 23:09
一:24
二:10
为什么总扎堆问问题?

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));
// }
}
}
}
}
}
}
}阿卜 发表于 2016-8-25 17:47
好像这个是最笨的做法……再想想
[mw_shl_code=java,true]public class Practice {
惊鸿游龙 发表于 2016-8-25 20:18
定义count的意义是什么?
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);
}阿卜 发表于 2016-8-26 11:42
题目不是要求解多少种组合吗……难道打印出来了一个一个数?
阿卜 发表于 2016-8-26 11:42
题目不是要求解多少种组合吗……难道打印出来了一个一个数?
阿卜 发表于 2016-8-26 11:42
题目不是要求解多少种组合吗……难道打印出来了一个一个数?
阿卜 发表于 2016-8-25 17:47
好像这个是最笨的做法……再想想
[mw_shl_code=java,true]public class Practice {

gsa798780633 发表于 2016-8-26 19:42
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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;
}
}
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |