网易笔试题解
这次网易的笔试难度是不是有点低啊,早知道多做一会选择题了,除了招行人生第一次A了三道,比起其他几个大厂的劝退笔试感觉良心了好多。
PS(是我运气好,遇到简单的那几题了吗,我是翻拍,买房和香槟)
附带解答
1.翻牌(只有多钟情况,边缘的都是被翻偶数次的,所以肯定是正面,只有中间的会被翻9次,会是反面,然后再考虑一下特殊情况)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t = sc.nextInt();
for(int i = 0;i < t;i++){
long n = sc.nextLong(),m = sc.nextLong();
if(n*m == 1)
System.out.println(1);
else if(n==2 || m==2)
System.out.println(0);
else if((n == 1 || m == 1))
System.out.println((m>n?m:n)-2);
else
System.out.println((n - 2) * (m - 2));
}
}
}
}
2.买房(同样是只有几种情况,只要让住户都挨一起,最小就肯定是0,最大肯定是交替排列)
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t = sc.nextInt();
for(int i = 0;i < t;i++){
long n = sc.nextLong(),m = sc.nextLong();
if(n*m == 1)
System.out.println(1);
else if(n==2 || m==2)
System.out.println(0);
else if((n == 1 || m == 1))
System.out.println((m>n?m:n)-2);
else
System.out.println((n - 2) * (m - 2));
}
}
}
}
2.买房(同样是只有几种情况,只要让住户都挨一起,最小就肯定是0,最大肯定是交替排列)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t = sc.nextInt();
for(int i = 0;i < t;i++){
long n = sc.nextLong(),k = sc.nextLong();
if(n - k < k){
System.out.print(0 + " ");
System.out.println(n - k);
}else if(k < 2){
System.out.print(0 + " ");
System.out.println(0);
}else{
System.out.print(0 + " ");
System.out.println(k - 1);
}
}
}
}
}
3.倒香槟(直接暴力只有90%,所以用一个栈记录下面的那层是不是已经满了,如果满了就不遍历了)
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t = sc.nextInt();
for(int i = 0;i < t;i++){
long n = sc.nextLong(),k = sc.nextLong();
if(n - k < k){
System.out.print(0 + " ");
System.out.println(n - k);
}else if(k < 2){
System.out.print(0 + " ");
System.out.println(0);
}else{
System.out.print(0 + " ");
System.out.println(k - 1);
}
}
}
}
}
3.倒香槟(直接暴力只有90%,所以用一个栈记录下面的那层是不是已经满了,如果满了就不遍历了)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt(),m = sc.nextInt();
int[] a = new int[n + 1];
int[] value = new int[n + 1];
Stack<Integer> s = new Stack<>();
for(int i = 1;i <= n;i++)
s.push(i);
for(int i = 1;i <= n;i++)
a[i] = sc.nextInt();
for(int i = 0;i < m;i++){
int b = sc.nextInt();
if(b == 2){
int x = sc.nextInt();
if(s.peek() < x)//如果这层以及以下的都已经满了,就跳过
continue;
int v = sc.nextInt();
while (x <= n && v > a[x] - value[x]){
v -= a[x] - value[x];
value[x] = a[x];
if(s.peek() == x)
s.pop();//如果这层和下面的都满了,就弹出,不再给这层以及以下灌酒
x++;
}
if(x <= n)
value[x] += v;
}else {
int k = sc.nextInt();
System.out.println(value[k]);
}
}
}
}
}
#网易##题解##笔试题目#public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt(),m = sc.nextInt();
int[] a = new int[n + 1];
int[] value = new int[n + 1];
Stack<Integer> s = new Stack<>();
for(int i = 1;i <= n;i++)
s.push(i);
for(int i = 1;i <= n;i++)
a[i] = sc.nextInt();
for(int i = 0;i < m;i++){
int b = sc.nextInt();
if(b == 2){
int x = sc.nextInt();
if(s.peek() < x)//如果这层以及以下的都已经满了,就跳过
continue;
int v = sc.nextInt();
while (x <= n && v > a[x] - value[x]){
v -= a[x] - value[x];
value[x] = a[x];
if(s.peek() == x)
s.pop();//如果这层和下面的都满了,就弹出,不再给这层以及以下灌酒
x++;
}
if(x <= n)
value[x] += v;
}else {
int k = sc.nextInt();
System.out.println(value[k]);
}
}
}
}
}