这次题目感觉比较简单,都是常规题,所以都AC了(本渣渣碰到难一点的就全跪了...)
第一题就是用哈希表统计次数,然后将最高频对于单词收集起来,进行排序,取第一个
package honor1;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String line = cin.nextLine();
String[] votes = line.split(",");
Map<String, Integer> counts = new HashMap<>();
int maxVote = 0;
for (String name : votes) {
if (!check(name)) {
System.out.println("error.0001");
return;
}
int vote = counts.getOrDefault(name, 0) + 1;
counts.put(name, vote);
maxVote = Math.max(maxVote, vote);
}
List<String> names = new ArrayList<>();
for (Map.Entry<String, Integer> entry : counts.entrySet()) {
String key = entry.getKey();
int value = entry.getValue();
if (value == maxVote) {
names.add(key);
}
}
Collections.sort(names);
System.out.println(names.get(0));
cin.close();
}
public static boolean check(String name) {
int len = name.length();
for (int i = 0; i < len; i++) {
char ch = name.charAt(i);
if (!Character.isLetter(ch))
return false;
if (i == 0) {
if (!Character.isUpperCase(ch))
return false;
} else {
if (!Character.isLowerCase(ch))
return false;
}
}
return true;
}
}
第二题用的库函数,直接将输入解释为BigInteger,然后用toString()方法转化为各种进制表示,再判断是否是回文串
package honor2;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String line = cin.nextLine();
BigInteger n = new BigInteger(line, 10);
boolean hasAnswer = false;
for (int radix = 2; radix <= 16; radix++) {
String nums = n.toString(radix);
if (isPalindrome(nums)) {
hasAnswer = true;
System.out.println(radix);
}
}
if (!hasAnswer)
System.out.println(-1);
}
public static boolean isPalindrome(String nums) {
int lo = 0, hi = nums.length() - 1;
while (lo < hi) {
char a = nums.charAt(lo);
char b = nums.charAt(hi);
if (a != b)
return false;
lo++; hi--;
}
return true;
}
}
第三题就是一致性哈希,代码写的烂,就不放了,主要讲下思路。可以用考虑用TreeMap保存所有存活redis节点,宕机理解为从TreeMap中删除相应redis节点,然后直接用ceiling(hash(token))找大于等于
hash(token)的第一个节点,若没有找到,说明被分配到TreeMap中的第一个节点。
还是贴下代码吧,好多冗余代码,也没设计好,轻喷...
package honor3;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
static Map<String, Integer> redisNum = new HashMap<>();
static Map<String, Integer> addRedisNum = new HashMap<>();
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String line = cin.nextLine();
String[] strs = line.split(":");
char command = strs[0].charAt(0);
String content = strs[1];
// System.out.println(command + ":" + content);
TreeMap<Integer, RedisNode> cluster = getCluster();
switch (command) {
case '1':
System.out.println(getNumber(content));
break;
case '2':
System.out.println(getTokenBelongNum(cluster, hash(content)));
break;
case '3':
System.out.println(getNumberWithDownServer(cluster, content));
break;
case '4':
System.out.println(getNumber(content));
break;
case '5':
System.out.println(afterInsertRedis(cluster, content));
break;
}
cin.close();
}
public static int getNumberWithDownServer(TreeMap<Integer, RedisNode> cluster,
String content) {
String[] parts = content.split(";");
String servers = parts[0], token = parts[1];
String[] nodes = servers.split(",");
for (String node : nodes) {
int number = getNumber(node);
// System.out.println(number);
cluster.remove(number);
}
// System.out.println(cluster.size());
return getTokenBelongNum(cluster, hash(token));
}
public static int getNumber(String nodeName){
return nodeName.startsWith("add") ? addRedisNum.get(nodeName) : redisNum.get(nodeName);
}
public static TreeMap getCluster() {
TreeMap<Integer, RedisNode> cluster = new TreeMap<>();
for (int i = 1; i <= 20; i++) {
int number = (i - 1) * 50;
String name = "redis_" + (i < 10 ? "0" + i : String.valueOf(i));
RedisNode node = new RedisNode(name, number, true);
cluster.put(number, node);
redisNum.put(name, number);
String addName = "add_" + name;
int addNum = 25 + (i - 1) / 2 * 50 + (i % 2 == 1 ? 0 : 500); addRedisNum.put(addName, addNum);
}
return cluster;
}
public static int afterInsertRedis(TreeMap<Integer, RedisNode> cluster,
String content) {
String[] parts = content.split(";");
String server = parts[0], token = parts[1];
String[] words = server.split("_");
int num = Integer.parseInt(words[2]);
for (int i = 1; i <= num; i++) {
String addName = getAddRedisName(i);
int addNum = addRedisNum.get(addName);
RedisNode node = new RedisNode(addName, addNum, true);
// System.out.println(node);
cluster.put(addNum, node);
}
// System.out.println(cluster.size());
return getTokenBelongNum(cluster, hash(token));
}
public static int getTokenBelongNum(TreeMap<Integer, RedisNode> cluster, int hashCode) {
// int hashCode = hash(token);
Integer number = cluster.ceilingKey(hashCode);
if (number == null) {
return cluster.firstKey();
}
return number;
}
public static int hash(String token) {
int sum = 0;
for (char ch : token.toCharArray()) {
sum += ch;
}
return sum % 999;
}
public static String getAddRedisName(int i) {
String name = "redis_" + (i < 10 ? "0" + i : String.valueOf(i));
String addName = "add_" + name;
return addName;
}
}
class RedisNode {
String name;
int number;
// 没用上
boolean isOnLine;
public RedisNode(String name, int number, boolean isOnLine) {
this.name = name;
this.number = number;
this.isOnLine = isOnLine;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public boolean isOnLine() {
return isOnLine;
}
public void setOnLine(boolean onLine) {
isOnLine = onLine;
}
@Override
public String toString() {
return "RedisNode{" +
"name='" + name + '\'' +
", number=" + number +
", isOnLine=" + isOnLine +
'}';
}
}
#荣耀笔试##荣耀2023秋招笔试交流讨论#