美团Java岗笔试

第一题是判断布尔
第二题是出租车派单匹配度最大值
有人分享下编程题的代码吗?
#美团##笔试题目#
全部评论
第一题,我用两个栈去做的,一个栈存true false,一个栈存or and,但是忘记考虑true false and这种情况的,就过了90的样例 第二题感觉是用动态规划去做,没写出来。
点赞 回复
分享
发布于 2019-09-18 17:11
import java.text.DecimalFormat; import java.util.*; public class Main{ static double Max=-1; static int[] vis,res; static int n;     public static void main(String[] args){         Scanner sc = new Scanner(System.in);         n = sc.nextInt();         vis = new int[n];         res = new int[n];         double[][] s = new double[n][n];         for(int i=0;i<n;i++)          for(int j=0;j<n;j++)          s[i][j]=sc.nextDouble();         int[] re = new int[n];         find(s,0,0,re);         DecimalFormat sf = new DecimalFormat("#0.00");         int tp = (int)Max;         System.out.println(sf.format(Max));         for(int i=0;i<n;i++)          System.out.println((i+1)+" "+(res[i]+1));     }     static void find(double[][] s,double max,int t,int[] re){      if(t==n){      if(max>Max){      for(int i=0;i<n;i++){      res[i]=re[i];      }      Max=max;      }      return ;      }      for(int i=0;i<n;i++){      if(vis[i]!=1){      vis[i]=1;      re[t]=i;      find(s,max+s[t][i],t+1,re);      vis[i]=0;      }      }     } }
点赞 回复
分享
发布于 2019-09-18 17:27
联想
校招火热招聘中
官网直投
我靠,我的是上次的数字转汉字,还是没做完😂
点赞 回复
分享
发布于 2019-09-18 17:09
第一题用栈做的,第二题用排列组合做的。
点赞 回复
分享
发布于 2019-09-18 17:10
为什么Java岗选择题一半C++的题,,,第一题A90,不知道还有什么没考虑,第二题,回溯剪枝?有想法不会做,hhh,,然后求问问答题的答案。。 感觉是凉了。。
点赞 回复
分享
发布于 2019-09-18 17:12
第二题暴力就可以了,我感觉反而是第一题难度大点,有点类似计算后缀表达式,不过不知道为啥只过了80%
点赞 回复
分享
发布于 2019-09-18 17:21
贴上菜鸡Java第一题代码,看到多行数据就改了下循环,脑子抽了没把代码里的 return 去掉,结束了才看见。。怪不得读不到多行的数据(awsl)。只过了50%,但我觉得应该能ac,欢迎指错讨论。 import java.util.*; public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         while(sc.hasNext()) {             String[] arr = sc.nextLine().split("\\s+");             // 首先把 error 的情况搞出来             // and or 不能开头,不能结尾             // true false 类别不能连续             // and or 类别也不能连续             boolean preIsAndOr = true;             boolean error = false;             for(int i = 0; i < arr.length; i ++) {                 String s = arr[i];                 if(preIsAndOr && ("and".equals(s) || "or".equals(s))) {                     error = true;                 } else if(!preIsAndOr && ("true".equals(s) || "false".equals(s))) {                     error = true;                 }                 preIsAndOr = "and".equals(s) || "or".equals(s) ? true : false;             }                          if(error || preIsAndOr) {                 System.out.println("error");                 continue;             }             // 能到这,说明序列本身已经没有问题了。             // 碰到 and 直接算             LinkedList<String> stack = new LinkedList<>();             for(int i = 0; i < arr.length; i ++) {                 String s = arr[i];                 if("true".equals(s) || "false".equals(s)) {                     stack.push(s);                 } else if("and".equals(s)) {                     String pre = stack.pop();                     String next = arr[++ i];                     if("false".equals(next) || "false".equals(pre)) {                         stack.push("false");                     } else {                         stack.push("true");                     }                 }             }             // 栈里的都当 or 处理             String res = "false";             for(String s : stack) {                 if("true".equals(s)) {                     res = "true";                     break;                 }             }             System.out.println(res);         }              }      }
点赞 回复
分享
发布于 2019-09-18 17:31

相关推荐

点赞 3 评论
分享
牛客网
牛客企业服务