2023 Web front-end logic interview questions

1. There are 9 small balls. It is known that one of the balls is heavier than the others. How to find the ball by weighing it only twice with a scale?

① Divide the 9 balls into three parts;

②Take out two of them and weigh them; there will be two situations

  • If the two small balls taken out are weighed, the weights are equal ; then two small balls are randomly taken out of the remaining small balls and weighed. If the weights are equal, the remaining one is different. If the weights of the small balls are not equal, the heavier one is the heavier one.
  • If the two small balls taken out are weighed, the weights are not equal ; then randomly take out two small balls from the one with the heavier weight and weigh them. If the weights are equal, the remaining one is not the same. If the weights of the same small balls are not equal, the heavier one is the heavier one.

2. There is an uneven incense that takes 1 hour to burn completely. There are two identical incense sticks. How to determine the 15 minutes?

① Both ends of the first incense are lit at the same time, and one end of the second incense is lit.

② When half an hour has passed after the first incense has been burned, and then the other end of the second incense is lit , the second incense will burn out in 15 minutes.

3. There are six numbers 1, 2, 2, 3, 4, and 5. It is required that 3 and 5 cannot be adjacent and 4 cannot be in the third position. How many permutations are there?

Note: ① Traverse from the minimum number to the maximum number to obtain the value;

       ②Convert the obtained value into String type;

       ③Judge that 4 cannot be in the third position through String's indexOf;

       ④Judge that "3" and "5" cannot be connected through the contains of String;

class Main {
 

  /***
   * 题目如下:用 1、2、2、3、4、5 这六个数字,用 java 写一个 main 函数,打印出所有不同的排列,如: 512234、412345
   * 等,要求:"4"不能在第三位,"3"与"5"不能相连。
   * 分析:
   * 1.不考虑条件,一共会产生多少个数字
   * 2.去筛选数字,满足要求的留下来
   * 3.将数字int类型转为String类型,利用String的方法来筛选 
   *
   */
 
      public static void main(String[] args) {
        int a=0;
          //六个数字所有组成的情况,从最小数到最大数
          for (int i = 122345; i <= 543221; i++) {
              if (method(String.valueOf(i))) {    //是否满足条件
                  System.out.println(i);
                  a++;
              }
          }
          
          System.out.println("最后结果:"+a);
  
      }
  
      /***
       * 判断数字是否满足要求
       * 
       * @param s
       */
      public static boolean method(String s) {
          String[] arr = { "1", "2", "3", "4", "5" };
  
          // 判断数字中是否含有12345五个数字
          for (int i = 0; i < arr.length; i++) {
              if (!s.contains(arr[i])) {
                  return false;
              }
          }
  
          // 判断数字中是否含有两个2,上面已经判断6个数字含有12345五个数字,只有一个数字有重复的机会,因此这里不需要判断数字2会出现3次甚至3次以上
          if (s.lastIndexOf("2") == s.indexOf("2")) {
              return false;
          }
  
          // 判断"4"不能在第三位
          if (s.indexOf("4") == 2) {
              return false;
          }
  
          // 判断"3"与"5"不能相连
          if (s.contains("35") || s.contains("53")) {
              return false;
          }
  
          //上面条件都满足,那么返回true
          return true;
      }
  
  }
 

Guess you like

Origin blog.csdn.net/weixin_41620505/article/details/132894026