zoukankan      html  css  js  c++  java
  • 天上掉馅饼--移动研究院2018校招笔试题

    题目:

    天上掉馅饼
    时间限制:C/C++语言 1000MS;其他语言 3000MS
    内存限制:C/C++语言 131072KB;其他语言 655360KB
    题目描述:
    大家都知道“天上不会掉馅饼”这句话,但是有一天,小明在回学校的路上,天上还真掉起了馅饼。小明的人品实在有点好,这馅饼会掉在小明身边10米的范围内。馅饼掉在地上显然就不能吃了,所以小明马上拿起他的背包去接。但是,小明是个技术宅,运动方面实在不太行,每秒钟只有在移动不超过1米的范围内接住馅饼。这条小路的图所示如下:

    现在,我们把问题简化一下,馅饼现在只掉在0~10这11个位置。开始时,小明站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置其中一个位置的馅饼。问小明最多能接多少个馅饼?请用Java/python/C++/C语言中的一种进行编程。
    输入
    测试数据多组,每组测试数据都会给出一共掉落的馅饼数N,接下里的N行给出每次馅饼掉落的坐标X和掉落的时间T。同一秒可能掉落多个馅饼。(0<N,T<100000)
    输出
    对于每个测试数据,输出一个整数,代表小明能够获得最多馅饼数量。

    样例输入
    6
    5 1
    4 1
    6 1
    7 2
    7 2
    8 3
    样例输出
    Case #1:
    The max number of pies is : 4

    思路:时间从0开始到没有馅饼掉落为止,遍历所有可能的移动方式,以及每种方式所获得的馅饼数,取最大值。

    代码(未在考试平台上测试):

     1 import java.util.*;
     2 
     3 public class Main{
     4     private void func(int endTime, int curTime, int mingPlace, Map<Integer, ArrayList<Integer>> timePlaceMap,
     5                      int curNum, ArrayList<Integer> numList) {
     6         if (curTime > endTime) {
     7             numList.add(curNum);
     8             return;
     9         }
    10 
    11         // 计算移动-1,0,+1步能获得的馅饼数目
    12         int num_here = 0, num_before = 0, num_behind = 0;
    13         if (timePlaceMap.containsKey(curTime+1)) {
    14             ArrayList<Integer> places = timePlaceMap.get(curTime + 1);
    15             System.out.println("places:" + places);
    16             for (int place: places) {
    17                 if (place == mingPlace) {
    18                     num_here++;
    19                 } else if (mingPlace > 0 && place == mingPlace - 1) {
    20                     num_before++;
    21                 } else if (mingPlace < 10 && place == mingPlace + 1) {
    22                     num_behind++;
    23                 }
    24             }
    25         }
    26         
    27         func(endTime, curTime+1, mingPlace, timePlaceMap,
    28                 curNum + num_here, numList);// 位置不变
    29         if (mingPlace > 0) {
    30             func(endTime, curTime+1, mingPlace-1, timePlaceMap,
    31                     curNum + num_before, numList);// 位置减1
    32         }
    33         if (mingPlace < 10) {
    34             func(endTime, curTime+1, mingPlace+1, timePlaceMap,
    35                     curNum + num_behind, numList);// 位置加1
    36         }
    37     }
    38 
    39     public static void main(String[] args) {
    40         Main mainClass = new Main();
    41         Scanner in = new Scanner(System.in);
    42         while(in.hasNext()) {
    43             String input = in.next();
    44             int n = Integer.parseInt(input);
    45             Map<Integer, ArrayList<Integer>> timePlaceMap = new HashMap<>();
    46             for (int i=0; i<n; i++) {
    47                 int place = Integer.parseInt(in.next());
    48                 int time = Integer.parseInt(in.next());
    49                 ArrayList<Integer> places = timePlaceMap.getOrDefault(time, new ArrayList<Integer>());
    50                 places.add(place);
    51                 timePlaceMap.put(time, places);
    52             }
    53             int endTime = Collections.max(timePlaceMap.keySet());
    54             ArrayList<Integer> numList = new ArrayList<>();
    55             mainClass.func(endTime, 0, 5, timePlaceMap, 0, numList);
    56             int maxNum = Collections.max(numList);
    57             System.out.println(" The max number of pies is : " + maxNum);
    58         }
    59         in.close();
    60     }
    61 }
  • 相关阅读:
    jquery多次上传同一张图片
    选中没有选中的复选框,匹配含有某个字符串的正则,json取值的两种方法,把变量定义在外面跟里面的区别
    点击div全选中再点击取消全选div里面的文字
    this指向问题
    js的querySelector跟querySelectorAll
    点击下箭头⤵️变上箭头⬆️来回切换的两种方法
    a标签的href值
    padding下中英文左右两端对齐
    map()函数
    设计模式
  • 原文地址:https://www.cnblogs.com/renzongxian/p/7784897.html
Copyright © 2011-2022 走看看