zoukankan      html  css  js  c++  java
  • 2019腾讯春招正式批笔试

    开发岗位笔试题(4月5日)

    第一题:

    题解:

     贪心算法,题解参考

     1 package testTecent;
     2 
     3 import java.util.Arrays;
     4 import java.util.Scanner;
     5 
     6 public class test1 {
     7     public static void main(String[] args) {
     8         Scanner in = new Scanner(System.in);
     9         while (in.hasNextInt()) {
    10 //            先读入一个整数n,再接着读入n个整数存入数组
    11             int sum = 0;
    12             int ans = 0;
    13             int i;
    14 
    15             int m = in.nextInt();
    16             int n = in.nextInt();
    17             int[] arr = new int[n];
    18             for (i = 0; i < n; i++)
    19                 arr[i] = in.nextInt();
    20 
    21             Arrays.sort(arr);
    22             if (arr[0] != 1) {
    23                 System.out.println(-1);
    24                 return;
    25             }
    26             while (true) {
    27                 if (sum >= m) {
    28                     System.out.println(ans);
    29                     return;
    30                 }
    31 
    32                 for (i = n-1; i >= 0; i--) {
    33                     if (arr[i] <= sum + 1) {
    34                         sum += arr[i];
    35                         ans++;
    36                         break;
    37                     }
    38                 }
    39             }
    40         }
    41     }
    42 }

    第二题:

     

     

    题解:

    只需要统计0的个数和1的个数,两者作差即可。因为到最后不可能同时剩下0和1,只能剩下两者中的一个。

    第三题:

     算法岗位笔试题:

    第一题:

     

    第二题:

     

    第三题:

     

     

    TALK IS CHEAP, SHOW ME THE CODE
  • 相关阅读:
    设计模式——策略模式
    LeetCode
    平凡世界里的万千思绪
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
  • 原文地址:https://www.cnblogs.com/greatLong/p/10662190.html
Copyright © 2011-2022 走看看