有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?

解决方案 »

  1.   


    public class Compose
    {
    public static void main(String[] args)
    {
    int a,b,c,count=0;
    for(int i=123;i<433;i++)
    {
    a=i/100; //求出百位数
    b=i%100/10; //求出十位数
    c=i%10;  //求出个位数
    if((a==b||b==c||a==c)||(a==0||a>4)||(b==0||b>4)||(c==0||c>4))
    {//a,b,c任意2个相等或者a,b,c任意一个数不是1--4之间的话说明不符合组合要求
    continue;
    }
    else
    {
    System.out.print(i+" ");
    count++;
    }
    }
    System.out.println("\ncount="+count);
    }
    }
      

  2.   

    总共有24种组合
    用循环写了一下,这样做应该还可以把。。public class Test { public static void main(String[] args) {
    int[] arr = new int[]{1,2,3,4};

    for(int i=0;i<arr.length; i++) {
    for(int j=0; j<arr.length; j++) {
    if(j==i) continue;
    for(int k=0; k<arr.length; k++) {
    if(k==j || k==i) continue;
    System.out.println(""+arr[i]+arr[j]+arr[k]);
    }
    }
    }
    }
    }
      

  3.   

    another:public class Test {    public static void main(String[] args) {
            int[] arr = new int[]{1,2,3,4};
            
            for(int i=0;i<arr.length; i++) {
                for(int j=0; j<arr.length; j++) {
                    for(int k=0; k<arr.length; k++) {
                        if(k==j || k==i|| i==j) continue;
                        System.out.println(""+arr[i]+arr[j]+arr[k]);
                    }
                }
            }
        }
    }
      

  4.   

    public class Test {
     
     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++) ...{
               
               //确保i、j、k三位互不相同
               if (i != j && j != k && i != k) ...{
                
                //计数器累加
                ++count;
                //输出当前组合出的三位数
                System.out.println("第" + count + "种组合方式:" + i + "" + j + "" + k);
               }
              }
             }
            }
            //输出统计信息
            System.out.println(" 共计" + count + "种三位数组合方式。");
     }
    }