实现功能:1:有100组数据,2:每组六个.每个数据的范围为1-20,3:每组中的数据均不同.但是组和组之间有可能出现相同的数字.甚至相同的组合.4:组内数字从小到大排列.例如: 01,02,03,09,14,15          13,14,15,16,17,18         . . .....现在想求出次数出现最多的三个数字的组合.(就是这三个数字在100组中出现最频繁)想请教高手这个算法怎么实现

解决方案 »

  1.   

    做一个20元素的数组,下标为数据,内容为次数。双for循环历遍,发现 一个就向数组中累加,最后对数组中数据做比较就可以了!
      

  2.   

    每组数据包含20种3数组合
    3数组合的可能性总共有1140种
    understand?
      

  3.   

    quote:最多的三个数字的组合.(就是这三个数字在100组中出现最频繁)
    比如发现12,3,9是出现次数最多的3个数字,则组合是(12,3,9)
    quote:2:每组六个.每个数据的范围为1-20,
    to gogon() :
    就按你的每种3而不是6数吧
    方法1:长度为1140的数组,依次填入每种3数组合出现的次数,最后挑选最大的。
    这样很可能每种组合次数出现1次,或2次,相对于100组数组,而且求出的仅是出现次数最多的组合而不是出现最多次数字的组合。
    方法2:将1140种3数组合编号。
    对每组数据,拥有20个编号。也就是总共2000个编号,统计这些编号中出现最多
    将1140种3数组合编号,对每组数据,拥有20个编号,什么意思?假设你的20个编号是数字1—20,那也解释不了为什么一个数组有20个编号,假设是1140种3数组合编号,那也应该是3种,何来20个编号?个人认为2楼正解,另对出现最多次数并列的数字(如可能12,3,4,5,7同时出现次数最多),在加一个随机从中选一个产生出现最多的三个数字的组合
      

  4.   

    至于算法,2楼的算法如果没有好的数据结构是常数次,可以考虑PRIORITY QUEUE效率为O(log(n))的数据结构,对所有数字按出现次数进行排序,去其中最多的3次
      

  5.   

    /**
      * an fault : topN(int[],int) method will
      * change the value of the argument int[]
      *
      */
    package com.yatium;
    import java.util.*;public class TestArray{
        /* print an arry */
        public static void printArr(int[] arr){
            for(int i=0;i<arr.length;i++)
                System.out.print(arr[i]+"   ");
            System.out.println();
        }
        /*clone an array */
        public static int[] copyArr(int[] arr){
            int[] tempArr=new int[arr.length];
            for(int i=0;i<arr.length;i++) tempArr[i]=arr[i];
            return tempArr;
        }
        /* return the index of the maximum value of an array */
        public static int maxArr(int[] arr){
            int i=0,j=1;
            for(;j<arr.length;j++) if(arr[j]>arr[i]) i=j;
            return i;
        }
         /* return the index of the minimum value of an array */
        public static int minArr(int[] arr){
            int i=0,j=1;
            for(;j<arr.length;j++) if(arr[j]<arr[i]) i=j;
            return i;
        }
        /* set the value with the given index of an array */
        public static void setArr(int[] arr,int index,int value){
            if(index>=arr.length) throw new ArrayIndexOutOfBoundsException();
            arr[index]=value;
        }
        /* return an array which contains indexes of top n sort by value in an array */
        public static int[] topN(int[] arr,int n){
            int[] indexArr=new int[n];
            for(int i=0;i<n;i++){
                indexArr[i]=maxArr(arr);
                setArr(arr,maxArr(arr),arr[minArr(arr)]);
            }
            return indexArr;
        }
    }package com.yatium;
    import java.util.*;
    import com.yatium.TestArray;public class Test45{    
        public static int validNum(){
            Random r=new Random();
            return r.nextInt(20)+1;
        }
        public static void main(String[] args){
            int[][] arr=new int[100][6];
            for(int i=0;i<100;i++)
                for(int j=0;j<6;j++)
                    arr[i][j]=validNum();
            for(int i=0;i<100;i++)
                Arrays.sort(arr[i]);
            int[] arr1=new int[20];
            for(int i=0;i<20;i++) arr1[i]=0;
            for(int i=0;i<100;i++)
                for(int j=0;j<6;j++)
                    arr1[arr[i][j]-1]++;
            int[] arr2=TestArray.copyArr(arr1);
            int[] indexArr=TestArray.topN(arr1,3);
            for(int i=0;i<3;i++)
                System.out.println("the numbers with most times of ocurrence fo top 3 : "+
                    (indexArr[i]+1)+"   "+arr2[indexArr[i]]);
            for(int i=0;i<20;i++) System.out.print(arr1[i]+"    ");
            System.out.println();
        }
    }    无聊,帮你写着玩的,呵呵。