A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© huangqili 中级黑马   /  2014-7-17 16:38  /  1213 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

题目:有1234四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?   
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 +"个三位数");
}
}


都过一遍,熟悉熟悉哈



3 个回复

倒序浏览
24个三位数
回复 使用道具 举报
我记得小时候做过类似的数学应用题,但怎么做的我已经忘了
回复 使用道具 举报
  1. public class Tests {
  2.         public static final int N = 4;
  3.         public static void main(String[] args){

  4.                 // 标记数字是否被使用过.
  5.                 // digits[i] == 0 ==>数字i+1未使用过
  6.                 // digits[i] == 1 ==>数字i+1已使用过
  7.                 int[] digits = new int[N];
  8.                 // digits初始化为0
  9.                 Arrays.fill(digits, 0);

  10.                 // 深度优先搜索求排列
  11.                 permute(digits, 3, 0);
  12.                
  13.         }
  14.        
  15.         /**
  16.          * @param n:还需要寻找几个数字
  17.          * @param num:当前已找到的数字
  18.          * */
  19.         public static void permute(int[] digits, int n, int num) {
  20.                 // 已经达到输出位数要求,输出排列好的数字
  21.                 if (n == 0)
  22.                         System.out.println(num);

  23.                 for (int i = 1; i <= N; i++) {
  24.                         // 数字i未被使用过
  25.                         if (digits[i - 1] == 0) {
  26.                                 // 标记i为已使用
  27.                                 digits[i - 1] = 1;
  28.                                 // 递归,寻找下一个数字
  29.                                 permute(digits, n-1, num * 10 + i);
  30.                                 // 递归完毕,标记i为未使用
  31.                                 digits[i - 1] = 0;
  32.                         }
  33.                        
  34.                 }
  35.         }
  36.                

  37. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马