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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© fantianfei 中级黑马   /  2015-7-2 09:22  /  606 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 fantianfei 于 2015-7-2 09:23 编辑

将写有数字k1,k2...kn的n个纸片放入口袋中,可以从口袋中去4次纸片,每次几下纸片上数字后将纸片再放回口袋。
如果四个数字的和是m,就输出true,否则就输出false。检查是否有输出true
  1. package org.mungo.algorithm;

  2. import java.util.Arrays;

  3. /**
  4. * 将写有数字k1,k2...kn的n个纸片放入口袋中,可以从口袋中去4次纸片,每次几下纸片上数字后将纸片再放回口袋。
  5. * 如果四个数字的和是m,就输出true,否则就输出false。检查是否有输出true的可能性。
  6. * @author Lily
  7. *
  8. */
  9. public class Draw {

  10.     /**
  11.      * 1.穷举
  12.      * n[a] + n[b] + n[c] + n[d] == m
  13.      *
  14.      * @param n
  15.      * @param m
  16.      * @return
  17.      */
  18.     private static boolean drawMethod(int[] n, int m) {
  19.         boolean result = false;
  20.         // 循环枚举所有可能

  21.         for (int a = 0; a < n.length; a++) {
  22.             for (int b = 0; b < n.length; b++) {
  23.                 for (int c = 0; c < n.length; c++) {
  24.                     for (int d = 0; d < n.length; d++) {
  25.                         if (n[a] + n[b] + n[c] + n[d] == m) {
  26.                             result = true;
  27.                         }
  28.                     }
  29.                 }
  30.             }
  31.         }

  32.         return result;
  33.     }

  34.     /**
  35.      * 递归二分查找
  36.      *
  37.      * @param Array
  38.      * @param low
  39.      * @param high
  40.      * @param key
  41.      * @return
  42.      */
  43.     private static boolean binarySearch(int Array[], int low, int high, int key) {
  44.         if (low <= high) {
  45.             int mid = (low + high) / 2;
  46.             if (key == Array[mid])
  47.                 return true;
  48.             else if (key < Array[mid])
  49.                 // 移动low和high

  50.                 return binarySearch(Array, low, mid - 1, key);
  51.             else if (key > Array[mid])
  52.                 return binarySearch(Array, mid + 1, high, key);
  53.         }
  54.         return false;
  55.     }

  56.     /**
  57.      * 2.n[d]=m - n[a] - n[b] - n[c]
  58.      *
  59.      * @param n
  60.      * @param m
  61.      * @return
  62.      */
  63.     private static boolean drawMethod2(int[] n, int m) {
  64.         boolean result = false;
  65.         Arrays.sort(n); // 进行排序


  66.         for (int a = 0; a < n.length; a++) {
  67.             for (int b = 0; b < n.length; b++) {
  68.                 for (int c = 0; c < n.length; c++) {
  69.                     if (binarySearch(n, 0, n.length - 1, m - n[a] - n[b] - n[c])) {
  70.                         result = true;
  71.                     }
  72.                 }
  73.             }
  74.         }

  75.         return result;
  76.     }

  77.     /**
  78.      * 3.n[c]+n[d]=m - n[a] - n[b]
  79.      *
  80.      * @param n
  81.      * @param m
  82.      * @return
  83.      */
  84.     private static boolean drawMethod3(int[] n, int m) {
  85.         boolean result = false;
  86.         int nn[] = new int[n.length*n.length] ;
  87.         // 枚举n[c]+n[d]的和

  88.         for (int c = 0; c < n.length; c++) {
  89.             for (int d = 0; d < n.length; d++) {
  90.                 nn[c * n.length + d] = n[c] + n[d];
  91.             }
  92.         }
  93.         Arrays.sort(nn);
  94.         for (int a = 0; a < n.length; a++) {
  95.             for (int b = 0; b < n.length; b++) {
  96.                 if (binarySearch(nn, 0, nn.length - 1, m - n[a] - n[b])) {
  97.                     result = true;
  98.                 }
  99.             }
  100.         }

  101.         return result;
  102.     }

  103.     public static void main(String[] args) {
  104.         int[] numbers = { 1, 3, 5 };
  105.         System.out.println(drawMethod(numbers, 10));
  106.         System.out.println(drawMethod2(numbers, 10));
  107.         System.out.println(drawMethod3(numbers, 9));
  108.     }

  109. }
复制代码

的可能性。

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

1 个回复

倒序浏览
循环原则上不能超过三个,要不然可读性非常差,还有循环得到想要的结果后就结束循环,提高效率
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马