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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈圳 高级黑马   /  2013-3-5 22:29  /  4319 人查看  /  25 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陈圳 于 2013-3-5 22:31 编辑

示例:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?   
但我想实现的是随意给予几个元素.限定组合个数,其实其组合...
求高级算法!..............................................................................................................
效率越高越好,代码越少越好.跪求{:soso_e109:}
另外...散币散币.每人/5  *120 +税==720...

25 个回复

倒序浏览

回帖奖励 +5 黑马币

你都27分了 这么快啊  我先得5J  再去写算法
回复 使用道具 举报
1 2 3 4  这太简单了 12  不过要是 a b c d 的话 取值[0-9]  
回复 使用道具 举报

回帖奖励 +5 黑马币

本帖最后由 明锦添 于 2013-3-6 01:13 编辑

package com.soft.test;

/**
* 实现方式,将数组的第一个元素拿出来排放在数组的第一个位置、第二个位置,到第n个位置,其它顺序不变,直到一轮排完,将原第一个元素放到最后,然后又从头往复。
*
* @author Administrator
*
*/
public class Algorithm {

        public static void main(String[] args) {
                int[] arrays = { 1, 2, 3, 4 };// 数组

                for (int i = 0; i < arrays.length; i++) {// 将原数组的第一个元素,重新排在第一位输出没有意义(也不方便循环输出),故直接输出。
                        System.out.print(arrays + " ");
                }

                System.out.println();
                insert(arrays);
        }

        public static void insert(int[] arrays) {
                int k = 0;
                for (int j = 0; j < arrays.length; j++) {
                        for (int i = 1; i < arrays.length; i++) {// 每次将第一个元素从第二个位置,第三个位置,第n...排列,输出。。
                                if (j == arrays.length - 1 && i == arrays.length - 1) // 将原数组的最后一个元素,重新排在最后位输出没有意义(与原数组相同),故直跳过。
                                        return;
                                show(arrays, i);
                        }
                        int n = arrays[0];// 一趟完成后,将原数组中的第一个元素移到数组末尾
                        for (k = 0; k < arrays.length - 1; k++) {
                                arrays[k] = arrays[k + 1];
                        }
                        arrays[k] = n;
                }
        }

        public static void show(int[] arrays, int newPos) {
                int number = arrays[0];
                for (int i = 1; i <= newPos; i++) {// 输出新数据插入位置前的数据,
                        System.out.print(arrays + " ");
                }
                System.out.print(number + " ");// 输出插入的数据
                for (int i = newPos + 1; i < arrays.length; i++) {// 输出插入位置及后面的数据
                        System.out.print(arrays + " ");
                }
                System.out.println();
        }
}  
运行结果:
1 2 3 4
2 1 3 4
2 3 1 4
2 3 4 1
3 2 4 1
3 4 2 1
3 4 1 2
4 3 1 2
4 1 3 2
4 1 2 3
1 4 2 3
1 2 4 3  
汗颜,看错题目了,我还以为是计算排列组合{:soso_e127:}



评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1 鼓励鼓励

查看全部评分

回复 使用道具 举报

回帖奖励 +5 黑马币

哎技术分都27了羡慕啊,不过确实简单
class Test
        {


                public static void main(String[] args)
             {
                        int[]arryArr =new int[]{1,2,3,4};

                        for(int i=0;i<4;i++)
                                for(int j=0;j<4;j++)
                                        for(int n=0;n<4;n++){
                                       
                                                System.out.println(( arryArr*100)+(arryArr[j]*10)+(arryArr[n]));
                                                
                                        }
             }
        }
回复 使用道具 举报
本帖最后由 门文通 于 2013-3-6 01:05 编辑
  1. class  San
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int[] arr=new int[]{4,6,7,2,5,3,9,3,3,3,9,0,2};//定义随意数组
  6.                 int m=san(arr);
  7.                 System.out.println("Hello World!.........."+m);
  8.         }
  9.         public static int san(int[] arr)
  10.         {
  11.                 for(int x=0;x<arr.length;x++)

  12.                 {
  13.                         if(arr[x]<0 || arr[x]>9)//判断是否为0-9之间 单位数最多到999   不是则返回1000
  14.                                 return 1000;
  15.                 }

  16.                 int n=0;
  17.                 int[] a=new int[10];
  18.                 for(int y=0;y<10;y++)
  19.                 {
  20.                         for(int x=0;x<arr.length;x++)
  21.                         {
  22.                                 if(arr[x]==y)
  23.                                         n++;//对数组中每个数字的个数计数  并存到另一个数组
  24.                                 
  25.                         }System.out.print(n);
  26.                         a[y]=n;
  27.                         n=0;
  28.                
  29.                 }
  30.                 int m=arr.length;
  31.                 for(int x=0;x<10;x++)//去除数组中重复元素后的剩余个数
  32.                 {
  33.                         if(a[x]!=0)
  34.                                 m=m-a[x]+1;
  35.                         
  36.                 }
  37.                 if(a[0]==0)
  38.                         return m*m*m;

  39.                 else
  40.                         return m*(m-1)*m;
  41.                         
  42.                
  43.         }
  44. }
复制代码
刚写的  作用是  把任意个0-9之间的int型数字 任意组合三位数  有多少
思想是    三位数,最高位不能为0 ,其他位置可以是0-9的数字,我先吧数组中0-9  不重复的个数求出来
            如是7个数   如果包涵0  那结果就是6*7*7  如果没有0  就是7*7*7
回复 使用道具 举报

回帖奖励 +5 黑马币

楼主有钱人,只想到了A43
回复 使用道具 举报

回帖奖励 +5 黑马币

这个得好好想想~
回复 使用道具 举报

回帖奖励 +5 黑马币

回就有金币?
回复 使用道具 举报

回帖奖励 +5 黑马币

这个我要好好想啊
回复 使用道具 举报

回帖奖励 +5 黑马币

我要好好考虑
回复 使用道具 举报
你太有爱了~~
回复 使用道具 举报

回帖奖励 +5 黑马币

好好想一下:)
回复 使用道具 举报

回帖奖励 +5 黑马币

这个问题我能回答 不过已经有人回答过了 那我就默默的看吧
回复 使用道具 举报

回帖奖励 +5 黑马币

楼主威武
回复 使用道具 举报

回帖奖励 +5 黑马币

!!!5个黑马币
回复 使用道具 举报

回帖奖励 +5 黑马币

5个币!
回复 使用道具 举报

回帖奖励 +5 黑马币

学习学习。5555
回复 使用道具 举报

回帖奖励 +5 黑马币

谢谢楼主的金币
回复 使用道具 举报

回帖奖励 +5 黑马币

这个得好好想想~
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马