华为20180815笔试题分享
第一题就不给答案了。
import java.util.Scanner;
public class Main {
static int[] count = new int[8];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag&&sc.hasNext()){
String s = sc.nextLine();
if (s.equals("-1,-1"))
flag = false;
else
getRes(s);
}
for (int i=0;i<count.length;i++){
System.out.println("["+(i+12)+","+(i+12+1)+")"+":"+count[i]);
}
}
private static void getRes(String s){
String[] sentence = s.split(",");
int start = Integer.parseInt(sentence[0])-12;
if (start<0)
start=0;
int end = Integer.parseInt(sentence[1])-12;
if (end>8)
end=8;
while (start<end){
count[start]++;
start++;
}
}
}
题目有错误。。
(+ (* 2 3) (^ 4))(2 3) 结果应该为 11 import java.util.*;
public class Main {
// 只通过80%
// (+ (* 2 3) (^ 4))
// (+ (* 2 3) (^ 4))(2 3)
// ((+ 2 3)
// ((+ 2 3))
// (^ (+ (* 2 3) (^ ((^ 4)))))
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String express = scanner.nextLine();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < express.length(); i++) {
char c = express.charAt(i);
if (c == ')') {
StringBuilder builder = new StringBuilder();
char top;
while (!stack.empty() &&(top = stack.pop()) != '(') {
builder.append(top);
}
String currExp = builder.reverse().toString();
// System.out.println(currExp);
String[] ops = currExp.split(" ");
if (ops.length == 2) {
int value = Integer.valueOf(ops[1]);
value++;
String count = String.valueOf(value);
for (int j = 0; j < count.length(); j++) {
stack.push(count.charAt(j));
}
} else if (ops.length == 3) {
int a = Integer.valueOf(ops[1]);
int b = Integer.valueOf(ops[2]);
int value = 0;
switch (ops[0]) {
case "+":
value = a + b;
break;
case "*": value = a * b; break;
}
String count = String.valueOf(value);
for (int j = 0; j < count.length(); j++) {
stack.push(count.charAt(j));
}
} else if (ops.length == 1) {
for (int j = 0; j < ops[0].length(); j++) {
stack.push(ops[0].charAt(j));
}
}
} else {
stack.push(c);
}
}
StringBuilder builder = new StringBuilder();
while (!stack.empty())
builder.append(stack.pop());
try {
System.out.println(Integer.valueOf(builder.reverse().toString()));
} catch (Exception e) {
System.out.println(-1);
}
}
}
第三题只过了80%,原因应该是注意的那个部分没有实现。
但是那个样例给的我也确实没有看懂,希望有AC的dalao能给分享一波代码
#华为##笔试题目##秋招#
查看4道真题和解析

