美团8.8笔试
网易的在15点,美团的在16点,想着把网易的快快答完去做美团的,结果网易的题也太难了吧。。就AC了一道,没办法了,网易笔试16:40结束,但是估计再来40分钟也做不出来,果断关了去做美团了。
美团的题还好,都有思路,大概写一写。
1 算平均评分,保留一位小数
很简单,不说了
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long val = 0;
int count = 0;
for (int i = 0; i < 5; i++) {
int ac = scanner.nextInt();
count +=ac;
val += ac *(i+1);
}
double v = ((double) val) / count;
System.out.println(Math.floor(v*10)/10);
}
} 2 优惠券
看懂了题目意思 也没什么难度,加就行了
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int va1 = 0;
int va2= 0;
for (int i = 0; i < n; i++) {
int v1 = scanner.nextInt();
int v2 = scanner.nextInt();
if(v1 > v2) {
va1 += v1;
va2 += (v1-v2);
}else {
va1+= v2;
}
}
System.out.println(va1 + " " + va2);
}
} 3 送花
题目给的例子太简单了,自己做的时候想错了,最后时间来不及改了,估计暴力搜索会超时就没尝试暴力,最后没AC,遗憾。
重新整理思路,补了一下代码,没办法测试了,如有问题欢迎指正。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Tree[] trees = new Tree[n];
trees[0] = new Tree(1);
for (int i = 1; i < n; i++) {
trees[i] = new Tree(i+1);
}
for (int i = 1; i < n; i++) {
int from = scanner.nextInt()-1;
int to = scanner.nextInt()-1;
int path = scanner.nextInt();
trees[from].nexts.add(trees[to]);
trees[to].father = trees[from];
trees[to].fromHome = trees[from].fromHome + path;
trees[to].fromFather = path;
}
int retVal1 = trees[0].getLengthFromThisToEveryNode();// 从花店到每个地址的距离总和
int val = 0;
int retVal2= 0;// 配送员一共走的路程总和
int maxTreeFromHome = 0;
Tree farTree = null;
for (Tree tree: trees){
retVal2 += tree.fromFather*2;
if(tree.fromHome >= maxTreeFromHome){
maxTreeFromHome = tree.fromHome;
farTree = tree;
}
}
while(farTree != null){
val += farTree.fromFather;
farTree = farTree.father;
}
retVal2-=val;
System.out.println(retVal1+" "+retVal2);
}
private static class Tree{
ArrayList<Tree> nexts;
Tree father;
int id;
int fromHome;//从家到这个地址的距离
int fromFather;//从上个地址到这个地址的距离
public int getLengthFromThisToEveryNode(){
int sum = this.fromHome;
for (Tree next :nexts)
sum += next.getLengthFromThisToEveryNode();
return sum;
}
public Tree(int id) {
this.nexts = new ArrayList<>();
this.id = id;
}
}
}
4 优惠券连连看,合并优惠券得奖励金
本来想着用一个数组去存这个优惠券金额,合并掉的就标记为-1,但是不知道为什么不行,只好用ArrayList,因为优惠券金额只有1到100,所以循环100次把对应金额的合并一下就行,不能合并的留到下一轮。
本题虽然通过了测试用例,但是评论有老哥指出问题,我只考虑从左侧合并,如果出现 1 1 1 这种,合并成 1 2 和 2 1 是不一样的,我后来又想了一下,暂时没想出来,后面再更吧。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(scanner.nextInt());
}
int count = 0;
for (int i = 1; i <= 100; i++) {
ArrayList<Integer> nlist = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
if(j== list.size()-1) {
nlist.add(list.get(j));
continue;
}
if(list.get(j) == i &&list.get(j).equals(list.get(j+1))) {
nlist.add(i + 1);
count ++;
j++;
}else
nlist.add(list.get(j));
}
list = nlist;
}
System.out.println(count);
}
} 5 黄黑树中不同颜色节点的深度
没有特别大的算法难度,但是不知道为什么我代码只过了91%,按理来说也没有很复杂的循环或递归,极端输入也不会出问题啊。诶,好迷
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Tree[] trees= new Tree[n+1];
for (int i = 1; i <= n; i++) {
trees[i] = new Tree(i,scanner.nextInt() != 1);
}
for (int i = 1; i <= n-1; i++) {
trees[scanner.nextInt()].sons.add(trees[i+1]);
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
sb.append(trees[i].calcDepth(0,trees[i].yellow)).append(" ");
}
System.out.println(sb.toString().trim());
}
private static class Tree{
ArrayList<Tree> sons;
int id;
boolean yellow;
public Tree(int id, boolean yellow) {
this.id = id;
this.yellow = yellow;
this.sons = new ArrayList<>();
}
public int calcDepth(int already,boolean initialColor){
int myDepth;
if(this.yellow ==initialColor){
myDepth = 0;
}else
myDepth = already;
for (Tree son : sons){
myDepth += son.calcDepth(already+1,initialColor);
}
return myDepth;
}
}
} 希望给个面试吧~
#笔试题目#
查看5道真题和解析