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

哒0o哒 2014-01-06 07:23:52
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();
}
}
}
...全文
56 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,395

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧