荣耀笔试 3道题,2道没有过。。。
1 题 加减法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.next();
boolean flag = true;
int sum = 0;
int i = 0;
while(i < line.length()) {
if (line.charAt(i) == '-') {
flag = false;
++i;
} else if (line.charAt(i) == '+') {
flag = true;
++i;
} else {
int num = 0;
while(i < line.length() && line.charAt(i) != '-' && line.charAt(i) != '+') {
num = num * 10 + line.charAt(i++) - '0';
}
sum += flag ? num: -num;
}
}
System.out.println(sum);
}
}
2 题 步数排优。不知道改怎么输出了,通过了50%
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] noResults = {"excellent is null", "excellent is null", "good is null", "bad is null", "There is no data."};
boolean[] hasResult = new boolean[4];
Scanner sc = new Scanner(System.in);
List<Entity> list = new ArrayList<>();
while (sc.hasNextLine()) {
list.add(new Entity(sc.nextLine()));
}
list.sort(null);
for (int i = 0; i < list.size(); ++i) {
Entity entity = list.get(i);
hasResult[entity.level] = true;
System.out.println(entity.say());
}
hasResult[1] |= hasResult[0];
for (int i = 1; i < hasResult.length; ++i) {
hasResult[0] |= hasResult[i];
if (!hasResult[i]) {
System.out.println(noResults[i]);
}
}
if (!hasResult[0]) {
System.out.println(noResults[4]);
}
}
static class Entity implements Comparable<Entity> {
static String[] levelStr = new String[]{"excellent", "excellent", "good", "bad"};
int level = 3;
String name;
int totalSteps;
List<Integer> stepList = new ArrayList<>();
Entity(String line) {
String[] ss = line.split(":");
name = ss[0];
String[] steps = ss[1].split(" ");
List<Integer> indexList = new ArrayList<>(); // > 3w
int over1w = 0; // > 1w
int over5t = 0; // (5000, 10000)
for (int i = 0; i < steps.length; ++i) {
int step = Integer.parseInt(steps[i]);
totalSteps += step;
stepList.add(step);
if (step >= 30000) {
indexList.add(i);
} else if (step >= 10000) {
over1w++;
} else if (step >= 5000) {
over5t++;
}
}
if (indexList.size() >= 4) {
for (int i = 1; i < indexList.size(); ++i) {
if (indexList.get(i) - indexList.get(i-1) <= 4) {
break;
}
level = 0;
return;
}
}
if (over1w >= 15) {
level = 1;
return;
}
if (over5t >= 15) {
level = 2;
}
}
public String say() {
StringBuilder sb = new StringBuilder();
sb.append(name).append(":").append(levelStr[level]).append(" ").append(totalSteps);
return sb.toString();
}
@Override
public int compareTo(Entity o) {
return level == o.level ? o.totalSteps - totalSteps : level - o.level;
}
}
}
/*
Gsy:35000 0 0 0 0 36000 0 0 0 0 0 40000 0 0 0 0 32000
Wj:12000 12000 12000 12000 12000 12000 12000 0 12000 12000 12000 12000 0 12000 12000 12000 12000 12000 12000
Jww:2000
Zzc:6000 6000 6000 6000 0 6000 6000 6000 0 0 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000
Dbw:3000
*/ 3 题 拓扑排序,检测有环。只能通过90.91%(在不检测环的情况下,添加了检测环,还是90.91%)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int[] deg = new int[27];
int[] hash = new int[128];
int id = 0;
char[] id2char = new char[27];
List[] edges = new List[line.length() + 3 >> 1];
for (int i = 0; i < line.length(); i += 4) {
char u = line.charAt(i);
char v = line.charAt(i + 2);
if (hash[u] == 0) {
hash[u] = ++id;
id2char[id] = u;
}
if (hash[v] == 0) {
hash[v] = ++id;
id2char[id] = v;
}
if (edges[hash[u]] == null) {
edges[hash[u]] = new ArrayList<>();
}
edges[hash[u]].add(hash[v]);
deg[hash[v]]++;
}
int n = id; // [1..n]
Queue<Integer> que = new PriorityQueue<>(); // 改为优先队列,正确率从20+%到90.91%
for (int i = 1; i <= n; ++i) {
if (deg[i] == 0) {
que.add(i);
}
}
StringBuilder cache = new StringBuilder();
while(!que.isEmpty()) {
int uid = que.remove();
cache.append(id2char[uid]).append(";");
if (edges[uid] == null) {
continue;
}
List<Integer> toList = (ArrayList<Integer>)edges[uid];
for (int vid: toList) {
if (--deg[vid] == 0) {
que.add(vid);
}
}
}
for (int uid = 1; uid <= n; ++uid) {
if (deg[uid] != 0) { // 存在环
for (int vid = uid + 1; vid <= n; ++vid) {
List<Integer> toList = (ArrayList<Integer>)edges[vid];
int idx = Collections.binarySearch(toList, uid);
if (idx >= 0) {
System.out.println(id2char[vid] + "|" + id2char[uid]);
return;
}
}
break;
}
}
System.out.println(cache);
}
} 
查看12道真题和解析