华为笔试题--麻将和牌问题

题目描述:
清一色是麻将番种之一,指由一种花色的序数牌组成的和牌.
数字1-9,每个数字最多有4张牌
我们不考虑具体花色,我们只看数字组合。
刻子:三张一样的牌;如: 111, 222, 333, ..., 999
顺子:三张连续的牌;如: 123, 234, 345, ..., 789
对子:两张相同的牌;如: 11, 22, 33, ..., 99
需要实现一个程序,判断给定牌,是否可以和牌(胡牌)。
和牌要求:

  • 麻将牌张数只能是 2, 5, 8, 11, 14
  • 给定牌可以组合成,除1个对子以外其他都是刻子或顺子
    举例: - "11" -> "11", 1对子,可以和牌
  • "11122233" -> "111"+"222"+"33", 2刻子,1对子,可以
  • "11223344567" -> "11"+"234"+"234"+"567", 1对子,3顺子,可以
    -> "123"+"123"+"44"+"567", 另一种组合,也可以
    输入描述:
    合法C字符串,只包含'1'-'9',且已经按从小到大顺序排好;字符串长度不超过15。同一个数字最多出现4次,与实际相符。
    输出描述:
    C字符串,"yes"或者"no"
    示例1
    输入
    2244
    输出
    24 //此处是试题原本模样,应该输出no

import java.util.Arrays;

/**
*
*/
package com.tc.recovery.test;

import java.util.Arrays;

/**
*
*/
public class Main3 {

  public static void main(String[] args) {
      String input = "1122334444789";
      int[] ints = newIndexArray(convertToIntArray(input.toCharArray()));
      System.out.println(Arrays.toString(ints));
      System.out.println(isHuPai(nextIndex(0, ints), ints) ? "yes" : "no");
  }

  public static boolean isHuPai(int index, int[] newcs) {
      //循环每一个位数
      for (int i = index; i < newcs.length; i = nextIndex(index, newcs)) {
          boolean isOk = false;
          for (int j = 0; j < 3; j++) {//三种分法组合
              int[] tmp = copyArray(newcs);
              if (exec(index, j, tmp)) {
                  isOk = isHuPai(nextIndex(index, tmp), tmp);
                  if (isOk) return true;
              }
          }
          if (!isOk) {
              System.out.println("失败");
              return false;
          }
      }
      return isFinish(newcs);
  }

  private static int nextIndex(int index, int[] newcs) {
      for (int i = index; i < newcs.length; i++) {
          if (newcs[i] > 0)
              return i;
      }
      return newcs.length;
  }


  public static boolean exec(int i, int j, int[] newcs) {
      if (j == 0) {
          return shunnzi(newcs, i);
      } else if (j == 1) {
          return kezi(newcs, i);
      } else if (j == 2) {
          return diuzi(newcs, i);
      }
      return false;
  }

  private static int[] copyArray(int[] is) {
      int[] newcs = new int[is.length];
      for (int i = 0; i < is.length; i++) {
          newcs[i] = is[i];
      }
      return newcs;
  }

  private static int[] newIndexArray(int[] is) {
      int[] newcs = new int[getMax(is) + 1];
      for (int i = 0; i < is.length; i++) {
          int index = is[i];
          newcs[index] += 1;
      }
      return newcs;
  }

  private static boolean shunnzi(int[] newcs, int i) {

      if (getByIndex(newcs, i - 2) != 0 && getByIndex(newcs, i - 1) != 0 && getByIndex(newcs, i) != 0) {
          newcs[i] = newcs[i] - 1;
          newcs[i - 1] = newcs[i - 1] - 1;
          newcs[i - 2] = newcs[i - 2] - 1;
          System.out.println(String.format("顺子 %d , %d ,%d ", i - 2, i - 1, i));
          return true;
      }

      if (getByIndex(newcs, i - 1) != 0 && getByIndex(newcs, i) != 0 && getByIndex(newcs, i + 1) != 0) {
          newcs[i] = newcs[i] - 1;
          newcs[i - 1] = newcs[i - 1] - 1;
          newcs[i + 1] = newcs[i + 1] - 1;
          System.out.println(String.format("顺子 %d , %d ,%d ", i - 1, i, i + 1));
          return true;
      }
      if (getByIndex(newcs, i) != 0 && getByIndex(newcs, i + 1) != 0 && getByIndex(newcs, i + 2) != 0) {
          newcs[i] = newcs[i] - 1;
          newcs[i + 1] = newcs[i + 1] - 1;
          newcs[i + 2] = newcs[i + 2] - 1;
          System.out.println(String.format("顺子 %d , %d ,%d ", i, i + 1, i + 2));
          return true;
      }

      return false;
  }

  private static boolean kezi(int[] newcs, int i) {
      if (newcs[i] > 2) {
          newcs[i] = newcs[i] - 3;
          System.out.println(String.format("克子 %d , %d ,%d ", i, i, i));
          return true;
      }
      return false;
  }

  private static boolean diuzi(int[] newcs, int i) {
      if (newcs[i] > 1) {
          newcs[i] = newcs[i] - 2;
          System.out.println(String.format("对子 %d , %d", i, i, i));
          return true;
      }
      return false;
  }

  private static int getByIndex(int[] newcs, int i) {
      if (i < 0 || i >= newcs.length) {
          return 0;
      }
      return newcs[i];
  }

  public static int[] convertToIntArray(char[] cs) {
      int[] array = new int[cs.length];
      for (int i = 0; i < cs.length; i++) {
          array[i] = Integer.valueOf(String.valueOf(cs[i]));
      }
      return array;
  }

  private static boolean isFinish(int[] is) {
      for (int c : is) {
          if (c > 0) {
              return false;
          }
      }
      return true;
  }

  private static int getMax(int[] is) {
      int r = is[0];
      for (int c : is) {
          if (c > r) {
              r = c;
          }
      }
      return r;
  }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。