求高手帮忙解决一下代码的核心算法,和代码注释

哒0o哒 哒0o哒 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
import java.util.Scanner;
public class Problem_1042 {
public static void main(String[]args) throws Exception {
Scanner scanner = new Scanner(System.in);
//湖的数目
int numberOfLake = 0;
//初始湖的钓鱼数量
int []fishOfLake;
//在湖里钓鱼5分钟,鱼量减少的数目
int []minusOfLake;
//钓鱼总共可用的时间
int totalTime;
//从一个湖到另一个湖所用的时间
int []timeOfUse;
//结果数组
int []resultOfTime;
while(true) {
String s = scanner.nextLine();
if(s.equals("0")) {
break;
}
numberOfLake = Integer.parseInt(s.trim());
fishOfLake = new int[numberOfLake];
resultOfTime = new int[numberOfLake];
minusOfLake = new int[numberOfLake];
timeOfUse = new int[numberOfLake - 1];
s = scanner.nextLine();
totalTime = Integer.parseInt(s.trim())*60;
s = scanner.nextLine();
String[]temp = s.trim().split("[ ]+");
for(int i=0;i<numberOfLake;i++) {
fishOfLake[i] = Integer.parseInt(temp[i]);
}
s = scanner.nextLine().trim();
temp = s.split("[ ]+");
for(int i=0;i<numberOfLake;i++) {
minusOfLake[i] = Integer.parseInt(temp[i]);
}
s = scanner.nextLine().trim();
temp = s.split("[ ]+");
for(int i=0;i<numberOfLake - 1;i++) {
timeOfUse[i] = Integer.parseInt(temp[i])*5;
}
//开始计算
int totalFish = 0;
int maxIndex = -1;
int maxNumber = 0;
//枚举
for(int i=0;i<numberOfLake;i++) {
int tempTotalTime = totalTime;
int[]tempFishOfLake = new int[numberOfLake];
for(int j=0;j<numberOfLake;j++) {
tempFishOfLake[j] = fishOfLake[j];
}
int[]tempResultOfTime = new int[numberOfLake];
for(int j=0;j<numberOfLake;j++) {
tempResultOfTime[j] = 0;
}
int tempTotalFish = 0;
maxNumber = -1;
maxIndex = -1;
for(int j=0;j<i;j++) {
tempTotalTime -= timeOfUse[j];
}
while(tempTotalTime > 0) {
maxNumber = 0;
maxIndex = -1;
for(int j=0;j<=i;j++) {
if(tempFishOfLake[j] > maxNumber) {
maxNumber = tempFishOfLake[j];
maxIndex = j;
}
}
if(maxIndex == -1) {
tempResultOfTime[0] += tempTotalTime;
break;
}
tempTotalTime -=5;
tempResultOfTime[maxIndex] +=5;
tempTotalFish += tempFishOfLake[maxIndex];
tempFishOfLake[maxIndex] -= minusOfLake[maxIndex];
}
if(tempTotalFish > totalFish) {
totalFish = tempTotalFish;
resultOfTime = tempResultOfTime;
}else if(tempTotalFish == totalFish) {
for(int j=0;j<tempResultOfTime.length;j++) {
if(tempResultOfTime[j] != resultOfTime[j]) {
if(tempResultOfTime[j] > resultOfTime[j]) {
for(int k=0;k<tempResultOfTime.length;k++) {
resultOfTime[j] = tempResultOfTime[j];
}
break;
}else {
for(int k=0;k<tempResultOfTime.length;k++) {
tempResultOfTime[j] = resultOfTime[j];
}
break;
}
}
}
}
}
//计算完毕,输出结果
for(int i=0;i<numberOfLake-1;i++) {
System.out.print(resultOfTime[i]+", ");
}
System.out.println(resultOfTime[numberOfLake-1]);
System.out.println("Number of fish expected: "+totalFish);
System.out.println();
}
}
}
...全文
评论
收藏 已收藏
分享
返回顶部
哒0o哒
Java 51,407 成员 86,137 内容