• 主页
  • Java SE
  • J2ME
  • Web开发
  • Java EE
  • Eclipse

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

哒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();
}
}
}
...全文
23 点赞 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
Java
创建于2007-09-28

1.8w+

社区成员

8.1w+

社区内容

Java相关技术讨论
社区公告
暂无公告