2021华为软件精英挑战赛的baseLine,Java版,仅供参考,无核心算法

avatar
avatar
云惠网小编
2845
文章
1
评论
2021年3月22日23:18:48 评论 62 次浏览 9607字阅读32分1秒
摘要

2021华为软件精英挑战赛给出我的baseLinepackage com.huawei.java.main;import java.util.*;public class Main { private static long TOTAL_COST = 0; private static long POWER_COST = 0; private static long SERVER_COST = 0; //服务器信息 public static Ha

本地运行成功

package com.huawei.java.main;
import java.util.*;
public class Main {
private static  long TOTAL_COST = 0;
private static  long POWER_COST = 0;
private static  long SERVER_COST = 0;
//服务器信息
public static HashMap<String, List<Integer>> serverInfos = new HashMap<>();
//虚拟机信息
public static HashMap<String, List<Integer>> vmInfos = new HashMap<>();
//请求信息
public static List<List<String>> requestInfos = new ArrayList<>();
//购买服务器信息
public static int serverNums = 0;
public static HashMap<Integer,List<Integer>> buyServerResource = new HashMap<>();
//部署信息
public static ArrayList<Integer> serverRunvms = new ArrayList<>();
//记录虚拟机运行在服务器上的信息
public static HashMap<String,List<Integer>> vmOnSevrer = new HashMap<>();
//记录输出
public static List<String> result = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//将服务器封装到集合serverinfos中
int serverNumbers = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < serverNumbers; i++) {
String serverInfo_init = scanner.nextLine();
getserverdata(serverInfo_init);
}
//将虚拟机封装到集合vminfos中
int vmNums = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < vmNums; i++) {
String vmInfo_init = scanner.nextLine();
getvmdata(vmInfo_init);
}
//购买服务器
buyServer();
//将每天请求序列封装到集合requestInfos中
int requestdays = Integer.parseInt(scanner.nextLine());
for (int day = 0; day < requestdays; day++) {
int oneDayReqNums = Integer.parseInt(scanner.nextLine());
requestInfos.clear();
for (int j = 0; j < oneDayReqNums; j++) {
String request_init = scanner.nextLine();
getRequest(request_init);
}
//进行匹配
match(day);
//耗能计算
serverPower();
}
//        System.out.println("程序运行时间:" + (endTime - startTime) + "ms");    //输出程序运行时间
//        System.out.println();
//        System.out.println("服务器费用:"+SERVER_COST);
//        System.out.println("服务器耗能费用:"+POWER_COST);
TOTAL_COST = SERVER_COST+POWER_COST;
//        System.out.println("总费用:"+TOTAL_COST);
for (String s : result) {
System.out.println(s);
}
}
/**
* 服务器能耗计算
*/
private static void serverPower() {
for (int i = 0; i < serverRunvms.size(); i++) {
if (serverRunvms.get(i) != 0){
POWER_COST += buyServerResource.get(i).get(5);
}
}
}
/**
* 为虚拟机匹配服务器
* @param day
*/
private static void match(int day) {
if (day != 0){
expansion();
}
migrate();
for (List<String> requestInfo : requestInfos) {
if (requestInfo.size() == 3){
int resourceEnough = createVM(requestInfo);
}else {
delVM(requestInfo);
}
}
}
//初始化购买虚拟机
public static void buyServer() {
String serverType = "hostVIHCW";
//        String serverType = "NV603";
int n = 10000;
for (int i = 0; i < n; i++) {
serverRunvms.add(0);
}
String initBuy = "(purchase, ";
initBuy += Integer.toString(1)+")";
//添加购买服务器的信息
result.add(initBuy);
String buyinfo= "("+ serverType +", ";
buyinfo += Integer.toString(n) + ")";
//添加购买服务器型号和数量的信息
result.add(buyinfo);
for (int i = 0; i < n; i++) {
buyServerResource.put(serverNums++, (ArrayList<Integer>) serverInfos.get(serverType));
SERVER_COST += serverInfos.get(serverType).get(4);
}
//        serverType = "hostUY41I";
        serverType = "NV604";
//        //添加购买服务器的信息
//        buyinfo= "("+ serverType +", ";
//        buyinfo += Integer.toString(serverNums) + ")\n";
//        //添加购买服务器型号和数量的信息
//        result.add(buyinfo);
//
//        for (int i = 0; i < n/2 ; i++) {
//            buyServerResource.put(serverNums++,serverInfos.get(serverType));
//            SERVER_COST += serverInfos.get(serverType).get(4);
//        }
}
/**
*删除虚拟机
* @param requestInfo
*/
private static void delVM(List<String> requestInfo) {
String reqVmId = requestInfo.get(1);
List<Integer> vmOnServerData = vmOnSevrer.get(reqVmId);
int serverId = vmOnServerData.get(0);
int vms = serverRunvms.get(serverId) - 1;
serverRunvms.set(serverId,vms);
List<Integer> resourcedata = buyServerResource.get(serverId);
if (vmOnServerData.size() == 5){
int needCore = vmOnServerData.get(1)/2,memory = vmOnServerData.get(2)/2;
ArrayList<Integer> serverData = new ArrayList<>();
int serverCoreA = buyServerResource.get(serverId).get(0) + needCore;
serverData.add(serverCoreA);
int serverCoreB = buyServerResource.get(serverId).get(1) + needCore;
serverData.add(serverCoreB);
int serverMemoryA = buyServerResource.get(serverId).get(2) + memory;
serverData.add(serverMemoryA);
int serverMemoryB = buyServerResource.get(serverId).get(3) + memory;
serverData.add(serverMemoryB);
serverData.add(resourcedata.get(4));
serverData.add(resourcedata.get(5));
buyServerResource.put(serverId,serverData);
}else{
int cores = vmOnServerData.get(1),memory = vmOnServerData.get(2);
if (vmOnServerData.get(3) == 1){
ArrayList<Integer> serverData = new ArrayList<>();
int serverCoreA = buyServerResource.get(serverId).get(0) + cores;
serverData.add(serverCoreA);
serverData.add(resourcedata.get(1));
int serverMemoryA = buyServerResource.get(serverId).get(2) + memory;
serverData.add(serverMemoryA);
serverData.add(resourcedata.get(3));
serverData.add(resourcedata.get(4));
serverData.add(resourcedata.get(5));
buyServerResource.put(serverId,serverData);
}else {
ArrayList<Integer> serverData = new ArrayList<>();
int serverCoreB = buyServerResource.get(serverId).get(1) + cores;
serverData.add(resourcedata.get(0));
serverData.add(serverCoreB);
int serverMemoryB = buyServerResource.get(serverId).get(3) + memory;
serverData.add(resourcedata.get(2));
serverData.add(serverMemoryB);
serverData.add(resourcedata.get(4));
serverData.add(resourcedata.get(5));
buyServerResource.put(serverId,serverData);
}
}
}
/**
* 创建虚拟机
* @param requestInfo
* @return -1:失败,1:成功
*/
private static int createVM(List<String> requestInfo) {
String VmType = requestInfo.get(1);
String reqVmId = requestInfo.get(2);
List<Integer> vmData = vmInfos.get(VmType);
int success = -1;
for (int i = 0; i < serverNums; i++) {
List<Integer> source = buyServerResource.get(i);
if(choseServer(source,vmData,i,reqVmId)){
int vms = serverRunvms.get(i) + 1;
serverRunvms.set(i,vms);
success = 1;
break;
}
}
return success;
}
private static boolean choseServer(List<Integer> resourcedata, List<Integer> vmData, int serverId, String reqVmId) {
int vmCores = vmData.get(0);
int vmMemeory = vmData.get(1);
int vmTwoNodes = vmData.get(2);
List<Integer> vmOnSevrerData = new ArrayList<> ();
int serverCoreA = resourcedata.get(0);
int serverCoreB = resourcedata.get(1);
int serverMemoryA = resourcedata.get(2);
int serverMemoryB = resourcedata.get(3);
if (vmTwoNodes == 1){ //双节点
int needCore = vmCores/2,needMemory = vmMemeory/2;
if (serverCoreA >= needCore && serverCoreB >= needCore && serverMemoryA >= needMemory && serverMemoryB >= needMemory){
ArrayList<Integer> serverData = new ArrayList<>();
serverCoreA -= needCore;
serverData.add(serverCoreA);
serverCoreB -= needCore;
serverData.add(serverCoreB);
serverMemoryA -= needMemory;
serverData.add(serverMemoryA);
serverMemoryB -= needMemory;
serverData.add(serverMemoryB);
serverData.add(resourcedata.get(4));
serverData.add(resourcedata.get(5));
buyServerResource.put(serverId,serverData);
//记录信息
vmOnSevrerData.add(serverId);
vmOnSevrerData.add(vmCores);
vmOnSevrerData.add(vmMemeory);
vmOnSevrerData.add(1);
vmOnSevrerData.add(2);
vmOnSevrer.put(reqVmId,vmOnSevrerData);
//记录输出
//                for (Integer resourcedatum : resourcedata) {
//                    System.out.println(resourcedatum);
//                }
result.add("("+Integer.toString(serverId)+")");
return true;
}else {
return false;
}
}else if (serverCoreA >= vmCores && serverMemoryA >= vmMemeory){//单节点A
ArrayList<Integer> serverData = new ArrayList<>();
serverCoreA -= vmCores;
serverData.add(serverCoreA);
serverData.add(resourcedata.get(1));
serverMemoryA -= vmMemeory;
serverData.add(serverMemoryA);
serverData.add(resourcedata.get(3));
serverData.add(resourcedata.get(4));
serverData.add(resourcedata.get(5));
buyServerResource.put(serverId,serverData);
vmOnSevrerData.add(serverId);
vmOnSevrerData.add(vmCores);
vmOnSevrerData.add(vmMemeory);
vmOnSevrerData.add(1);
vmOnSevrer.put(reqVmId,vmOnSevrerData);
//记录输出
result.add("("+Integer.toString(serverId)+", A)");
return true;
}else if (serverCoreB >= vmCores && serverMemoryB >= vmMemeory){//单节点B
ArrayList<Integer> serverData = new ArrayList<>();
serverCoreB -= vmCores;
serverData.add(resourcedata.get(0));
serverData.add(serverCoreA);
serverMemoryB -= vmMemeory;
serverData.add(resourcedata.get(2));
serverData.add(serverMemoryA);
serverData.add(resourcedata.get(4));
serverData.add(resourcedata.get(5));
buyServerResource.put(serverId,serverData);
vmOnSevrerData.add(serverId);
vmOnSevrerData.add(vmCores);
vmOnSevrerData.add(vmMemeory);
vmOnSevrerData.add(2);
vmOnSevrer.put(reqVmId,vmOnSevrerData);
//记录输出
result.add("("+Integer.toString(serverId)+", B)");
return true;
}
return false;
}
//迁移虚拟机
private static void migrate() {
String s = "(migration, 0)";
result.add(s);
}
//扩容服务器
private static void expansion() {
String s = "(purchase, 0)";
result.add(s);
}
/**
* 解析服务器
* @param serverInfo_init
*/
public static void getserverdata(String serverInfo_init) {
List<Integer> list = new ArrayList<Integer>();
String serverType = "";
String[] serverInfo_init_split = serverInfo_init.split(", ");
serverType = serverInfo_init_split[0].substring(1);
list.add(Integer.parseInt(serverInfo_init_split[1])/2);
list.add(Integer.parseInt(serverInfo_init_split[1])/2);
list.add(Integer.parseInt(serverInfo_init_split[2])/2);
list.add(Integer.parseInt(serverInfo_init_split[2])/2);
list.add(Integer.parseInt(serverInfo_init_split[3]));
list.add(Integer.parseInt(serverInfo_init_split[4].substring(0, serverInfo_init_split[4].length()-1)));
serverInfos.put(serverType, list);
}
/**
* 解析虚拟机
* @param vmInfo_init
*/
public static void getvmdata(String vmInfo_init){
List<Integer> list = new ArrayList<Integer>();
String vmType = "";
String[] vmInfo_init_split = vmInfo_init.split(", ");
vmType = vmInfo_init_split[0].substring(1);
for (int i = 1; i < 3; i++) {
list.add(Integer.parseInt(vmInfo_init_split[i]));
}
list.add(Integer.parseInt(vmInfo_init_split[3].substring(0, vmInfo_init_split[3].length()-1)));
vmInfos.put(vmType,list);
}
/**
* 解析请求序列
* @param request_init
*/
public static void getRequest(String request_init){
List<String> innerlist = new ArrayList<>();
String opName = "";
String[] request_init_split = request_init.split(", ");
opName = request_init_split[0].substring(1);
if ("add".equals(opName)){
innerlist.add(opName);
innerlist.add(request_init_split[1]);
innerlist.add(request_init_split[2].substring(0,request_init_split[2].length()-1));
}else{
innerlist.add(opName);
innerlist.add(request_init_split[1].substring(0,request_init_split[1].length()-1));
}
requestInfos.add(innerlist);
}
}

给出我的baseLine

2021华为软件精英挑战赛

可是提交后总是报部署虚拟机超出服务器资源限制。改编于知乎:ddmm

有兴趣的拿去看看吧!

本文转自 https://blog.csdn.net/qq_37795916/article/details/115023354

腾讯云618
avatar
2w 字长文爆肝 JVM 经典面试题!太顶了! java

2w 字长文爆肝 JVM 经典面试题!太顶了!

如果你是中高级程序员,那我相信你一定被面试官问过JVM。下次再被问到JVM,你直接把老周的这篇文章丢给他吧!话不多说,让我们直接进入主题吧。JVM内存结构,常见异常,调优参数,调优...
JAVA初窥-DAY08 java

JAVA初窥-DAY08

JAVA初窥-DAY08面向过程与面向对象实例化及调用方法和成员变量面向过程与面向对象面向过程:注重的是某件事情过程中的每一个步骤的实现。面向对象:把面向过程中的每一个步骤交给一个...
腾讯云618
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: