1,2,3,4四个数,可以组成多少个不同三位数?不能重复使用一个数,即不能有112,334,之类的一个数字在一个数中出现两次。
  要求用程序求出结果。

解决方案 »

  1.   


    public class Test6 {
    public static void main(String[] args) {
    int sum = 0;
    int[] a = { 1, 2, 3, 4 };
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a.length; j++) {
    if (a[j] == a[i])
    continue;
    for (int k = 0; k < a.length; k++) {
    if (a[k] == a[i] || a[k] == a[j])
    continue;
    System.out.println(a[i] + "" + a[j] + "" + a[k]);
    sum++;
    }
    }
    }
    System.out.println("共有" + sum + "组数");
    }}
      

  2.   

    output:
    ----------------
    123
    124
    132
    134
    142
    143
    213
    214
    231
    234
    241
    243
    312
    314
    321
    324
    341
    342
    412
    413
    421
    423
    431
    432
    共有24组数
      

  3.   

    学习,也可以用以下代码:
    String str="1234";
    char[] ch=str.toCharArrays();
    int index;
    for(int i=0;i<str.length();i++)
    {
        if(char[i]==char[i+1]){
            continue;
         }
        ........后面方法与上面类似。当然我的这个就是一个想法,没有去编译。
    。应该很不够完善。
    }
      

  4.   


    int[] a = {1,2,3,4};
    int count=0;
    for(int i=0;i<a.length;i++){
    for(int j=0;j<a.length;j++){
    for(int z=0;z<a.length;z++){
    if(a[i]!=a[j] && a[j]!=a[z] && a[i]!=a[z]){
    count++;
    System.out.print(a[i]+""+a[j]+""+a[z]+" ");
    }
    }
    }

    }
    System.out.println();
    System.out.println("个数:"+count);
    123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432 
    个数:24