黑马程序员技术交流社区
标题:
编程题,大家来试试吧
[打印本页]
作者:
huangqili
时间:
2014-7-17 16:38
标题:
编程题,大家来试试吧
题目:有
1
、
2
、
3
、
4
四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
public class lianxi11 {
public static void main(String[] args) {
int count = 0;
for(int x=1; x<5; x++) {
for(int y=1; y<5; y++) {
for(int z=1; z<5; z++) {
if(x != y && y != z&& x != z) {
count ++;
System.out.println(x*100 +y*10 + z );
}
}
}
}
System.out.println("
共有
" + count +"
个三位数
");
}
}
都过一遍,熟悉熟悉哈
作者:
meibinlove
时间:
2014-7-17 16:57
24个三位数
作者:
meibinlove
时间:
2014-7-17 16:59
我记得小时候做过类似的数学应用题,但怎么做的我已经忘了
作者:
fantacyleo
时间:
2014-7-17 17:41
public class Tests {
public static final int N = 4;
public static void main(String[] args){
// 标记数字是否被使用过.
// digits[i] == 0 ==>数字i+1未使用过
// digits[i] == 1 ==>数字i+1已使用过
int[] digits = new int[N];
// digits初始化为0
Arrays.fill(digits, 0);
// 深度优先搜索求排列
permute(digits, 3, 0);
}
/**
* @param n:还需要寻找几个数字
* @param num:当前已找到的数字
* */
public static void permute(int[] digits, int n, int num) {
// 已经达到输出位数要求,输出排列好的数字
if (n == 0)
System.out.println(num);
for (int i = 1; i <= N; i++) {
// 数字i未被使用过
if (digits[i - 1] == 0) {
// 标记i为已使用
digits[i - 1] = 1;
// 递归,寻找下一个数字
permute(digits, n-1, num * 10 + i);
// 递归完毕,标记i为未使用
digits[i - 1] = 0;
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2