关注
package huawei;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Q0003_serverArrange {
/*
* 0:无服务器
* 1:有服务器
* -1:服务器宕机
*
* 初始服务器:以 0位初始,50为间隔,
* 增添服务器:以25为初始,若加500不溢出,则加500,否则加50
*/
public static void main(String[] args) throws IOException {
System.setIn(new BufferedInputStream(new FileInputStream("resource/huawei0003.txt")));
Scanner sc = new Scanner(System.in);
String[] command = sc.nextLine().split(":");
int[] servers = new int[1000];
arrageServer(servers, 20, false);
int index;
String[] temp;
switch (command[0]) {
case "1":
temp = command[1].split("_");
index = Integer.parseInt(temp[temp.length - 1]);
System.out.println((index - 1) * 50);
break;
case "2":
index = getHash(command[1]);
System.out.println(findServer(servers, index));
break;
case "3":
//处理宕机服务器
temp = command[1].split(";");
String[] shutdowns = temp[0].split(",");
for (int i = 0; i < shutdowns.length; i++) {
String[] curTemp = shutdowns[i].split("_");
shutdownServer(servers, Integer.parseInt(curTemp[curTemp.length - 1]));
}
index = getHash(temp[temp.length - 1]);
System.out.println(findServer(servers, index));
break;
case "4":
temp = command[1].split("_");
index = Integer.parseInt(temp[temp.length - 1]);
System.out.println((index - 1) * 50 + 25);
break;
case "5":
temp = command[1].split(";");
String[] addServers = temp[0].split("_");
arrageServer(servers, Integer.parseInt(addServers[addServers.length - 1]), true);
index = getHash(temp[1]);
System.out.println(findServer(servers, index));
break;
}
}
// 计算token的哈希值
public static int getHash(String str) {
int res = 0;
for (int i = 0; i < str.length(); i++) {
res += (int) str.charAt(i);
res %= 999;
}
return res;
}
// 查找分配服务器位置
public static int findServer(int[] arr, int index) {
if (arr[index] != 0)
return index;
while (true) {
index = (index + 1) % arr.length;
if (arr[index] == 1)
return index;
}
}
// 设置宕机服务器
public static void shutdownServer(int[] arr, int index) {
arr[(index - 1) * 50 + 0] = -1;
}
// 安排服务器
public static void arrageServer(int[] arr, int num, boolean isAdd) {
int gap = 50;
if (!isAdd) {
//首次添加
for (int i = 0; i < num; i++) {
arr[i * gap] = 1;
}
} else {
int index = 25;
arr[index] = 1;
for (int i = 1; i < num; i++) {
index = index + 500 > 1000 ? index - 450 : index + 500;
arr[index] = 1;
}
}
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
转发
05-24 20:49
河南科技学院新科学院 计算机类 点赞 评论 收藏
转发
牛客热帖
正在热议
# 和牛牛一起刷题打卡 #
5076次浏览 461人参与
# 机械制造薪资爆料 #
341835次浏览 4059人参与
# 牛客帮帮团来啦!有问必答 #
992678次浏览 15354人参与
# 通信硬件薪资爆料 #
242956次浏览 2286人参与
# 腾讯工作体验 #
147038次浏览 1410人参与
# 你觉得机械有必要实习吗 #
10418次浏览 143人参与
# 如何写一份好简历 #
301900次浏览 4334人参与
# 秋招开了,你想投哪些公司呢 #
130091次浏览 3393人参与
# 实习必须要去大厂吗? #
18435次浏览 265人参与
# 你的简历改到第几版了 #
323681次浏览 4865人参与
# 机械制造2024笔面经 #
410585次浏览 6059人参与
# 毕业租房也有小确幸 #
32660次浏览 1988人参与
# 2022毕业生求职现身说法 #
20787次浏览 313人参与
# 0offer互助地 #
53528次浏览 559人参与
# 晒一晒我的offer #
3658085次浏览 56993人参与
# 2022届毕业生现状 #
337738次浏览 4598人参与
# 为什么国企只招应届生 #
56375次浏览 411人参与
# 视觉/交互/设计岗位评价 #
3381次浏览 61人参与
# 产品人专业大盘点 #
15143次浏览 120人参与
# 浅聊一下我实习的辛苦费 #
93789次浏览 920人参与