黑马程序员技术交流社区
标题:
java 编程题:有1、2、3、4四个数字,
[打印本页]
作者:
sabrina妖儿
时间:
2015-11-11 20:59
标题:
java 编程题:有1、2、3、4四个数字,
编程题:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
作者:
olivec
时间:
2016-2-13 19:23
public class Demo3 {
private static final int N = 3;
public static void main(String[] args) {
char[] chs = { '1', '2', '3', '4' };
char[] cs = new char[N];
print(chs, N, cs, 0);
}
public static void print(char[] chs, int n, char[] cs, int count) {
if (count == N) {
System.out.println(new String(cs));
return;
}
for (int j = 0; j < chs.length; j++) {
if (!check(cs, chs[j])) {
cs[count] = chs[j];
print(chs, n, cs, count+1);
cs[count]=0;
} else {
continue;
}
}
}
public static boolean check(char[] chs, char c) {
for (char ch : chs) {
if (c == ch) {
return true;
}
}
return false;
}
}
作者:
davy0119
时间:
2016-2-13 20:00
public class Demo {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++)
for (int k = 1; k <= 4; k++)
if (i != j && j != k && i != k) {
count += 1;
System.out.println(i*100 + j*10 + k);
}
System.out.println("共" + count + "个三位数");
}
}
作者:
ApiceLove
时间:
2016-2-14 12:46
这不就是高中的排列组合知识吗?
作者:
bolt
时间:
2016-2-14 12:52
olivec 发表于 2016-2-13 19:23
public class Demo3 {
private static final int N = 3;
有点看不懂啊 注释一下吧。
作者:
刺客015
时间:
2016-2-14 21:05
public class HundredNumber{ private int[] a={1, 2, 3, 4};//定义数组并赋值 private int num=0;//定义组成三位数的个数 private int hundredNum=0;//定义组成的三位数 //hundred-百位, tens-十位, units-个位 public int hundNumber(int hundred, int tens, int units){ return 100*hundred+10*tens+1*units; } public static void main(String[] args){ HundredNumber hn=new HundredNumber(); for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ for(int m=0; m<4; m++){ if(hn.a[i]!=hn.a[j]&&hn.a[j]!=hn.a[m]&&hn.a[m]!=hn.a[i]){ hn.hundredNum=hn.hundNumber(hn.a[i], hn.a[j],hn.a[m]); hn.num++; System.out.println(hn.hundredNum); } } } } System.out.println("Total: "+hn.num);
作者:
刺客015
时间:
2016-2-14 21:07
public class HundredNumber{
private int[] a={1, 2, 3, 4};//定义数组并赋值
private int num=0;//定义组成三位数的个数
private int hundredNum=0;//定义组成的三位数
//hundred-百位, tens-十位, units-个位
public int hundNumber(int hundred, int tens, int units){
return 100*hundred+10*tens+1*units;
}
public static void main(String[] args){
HundredNumber hn=new HundredNumber();
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int m=0; m<4; m++){
if(hn.a[i]!=hn.a[j]&&hn.a[j]!=hn.a[m]&&hn.a[m]!=hn.a[i]){
hn.hundredNum=hn.hundNumber(hn.a[i], hn.a[j],hn.a[m]);
hn.num++;
System.out.println(hn.hundredNum);
}
}
}
}
System.out.println("Total: "+hn.num);
作者:
hexinchun
时间:
2016-2-14 23:01
都是大神
作者:
15856681986
时间:
2016-2-15 17:01
都是大神
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2