美文网首页
算法总结

算法总结

作者: 阳光男孩joe | 来源:发表于2018-10-18 17:23 被阅读11次
       @Test
        public void addition_isCorrect() {
            int[] count = {10000, 121, 4433, 100, 121, 10};
            int num = findRepeatednumber(count);
            System.out.println(num);
    
            int[] a = {1, 2, 3, 4, 5};
            int[] b = {2, 3, 4, 5, 6, 7};
            int[] c = reverseMergeSortArray(a, b);
            for (int i = 0; i < c.length; i++) {
                System.out.println(c[i]);
            }
        }
        //假设数组长度10000,数组中的整数不大于10000
        //从数组中找出唯一重复两次的数字
        public int findRepeatednumber(int[] arr) {
            int[] count = new int[10001];
            for (int i = 0; i < arr.length; i++) {
                if (1 == count[arr[i]]) {
                    return arr[i];
                } else {
                    count[arr[i]]++;
                }
            }
            return 0;
        }
        //按倒序合并两个排序好的数组
        public int[] reverseMergeSortArray(int[] a, int[] b) {
            int i = a.length - 1;
            int j = b.length - 1;
            int k = 0;
            int[] c = new int[a.length + b.length];
            while (i >= 0 && j >= 0) {
                if (a[i] > b[j]) {
                    c[k++] = a[i--];
                } else {
                    c[k++] = b[j--];
                }
            }
            while (i >= 0) {
                c[k++] = a[i--];
            }
            while (j >= 0) {
                c[k++] = b[j--];
            }
            return c;
        }
    
    

    2.斐波那契数列

    1,1,2,3,5,8,13,21,34,...算出第20个数?

        /**
         * 请第 n个数是什么
         */
        public int fbnNum(int n) {
            if (n == 1 || n == 2) {
                return 1;
            } else {
                return fbnNum(n - 1) + fbnNum(n - 2);
            }
        }
    

    3. 一个操场,5个跑道,用5个人跑一次可以排出来12345名,假设每个人每次跑的成绩都是一样的,但只能知道这次跑步的名次,无法知道具体的时间成绩,现在有25个人,要找出其中的123名,求最少需要使用多少次操场。

    答案:7
    转自:https://blog.csdn.net/u010187139/article/details/45618535
    思路:

    1. 5人一组比赛,得出每组第一名,比赛5次;
    2. 将第一名的5个人比赛一次得出第一名,由于要得出前三名,所以这次比赛四名和五名及所在的组都慢,就不考虑了,比赛6次;
    3. 在2中的第一名所在的组可能包含第二名和第三名记为 A2,A3,在2中第二名所在的组可能含第三名记为 B2,在加上2中的第三名共5人,在比赛一次得出前两名,即为25人中的2,3名。比赛了7次。

    相关文章

      网友评论

          本文标题:算法总结

          本文链接:https://www.haomeiwen.com/subject/xeeszftx.html