Gone Fishing

原题网址

 

代码已经写出来了,自己测试的时候没有问题,提交上去之后反馈了我一个Runtime error  一口老血啊!

找了半天还是没找到可能越界啊啥的地方

import java.util.Scanner;
import java.util.Arrays;
import java.util.Queue;
import java.util.LinkedList;
//import java.math.*;
//import java.util.Arrays;
public class Main {
    public static void main(String[] args){
        Queue<Question> question=new LinkedList<Question>();
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n!=0){
            int h=sc.nextInt();
            int fi[]=new int[n];
            int di[]=new int[n];
            int ti[]=new int[n-1];
            for(int i=0;i<n;i++) fi[i]=sc.nextInt();
            for(int i=0;i<n;i++) di[i]=sc.nextInt();
            for(int i=0;i<n-1;i++)ti[i]=sc.nextInt()*5;
            Question qu=new Question(n,h,fi,di,ti);
            question.add(qu);
            n=sc.nextInt();
        }
        sc.close();
        while(!question.isEmpty()){
            Question next=question.remove();
            next.doSomething();
            if(next.answer!=null)
                System.out.print(next.answer[0]*5);
            for(int i=1;i<next.answer.length;i++)
                System.out.print(", "+next.answer[i]*5);
            //for(int i:next.answer)
                //System.out.print(i*5+",");
            System.out.println("\nNumber of fish expected: "+next.number+"\n");
            //System.out.println(next.ti[next.ti.length-1]);
        }
    }
}
class Question{
    int n;
    int h;
    int[] fi;
    int[] di;
    int[] ti;
    int[] answer;
    int number;//用于保存最多可以钓多少鱼
    private int temp;
    Question(int a,int b,int[] c,int[] d,int [] e){
        n=a;h=b;fi=Arrays.copyOf(c,c.length);di=Arrays.copyOf(d, d.length);ti=Arrays.copyOf(e, e.length);
    }
    public void doSomething(){
        int max=0;int[] temp0 = null;
        for(int i=1;i<=n;i++){
            int x=h*60;
            for(int j=0;j<i-1;j++) x-=ti[j];
            int[] temp1=getBest(i,x,Arrays.copyOf(fi, i),Arrays.copyOf(di, i));
            if(temp>max){
            max=temp;    
            temp0=temp1;
            }
        }
        number=max;
        answer=temp0;
    }
    private int[] getBest(int n,int h,int[] fi,int[] di){
        temp=0;int[] list=new int[this.n];
        for(int i=0;i<h;i+=5){
            int max=0;int flag=0;
            for(int j=0;j<n;j++){
                if(max<fi[j]){
                    flag=j;max=fi[j];
                }
            }
            temp+=fi[flag];
            list[flag]++;
            //h-=5;
            fi[flag]=fi[flag]>di[flag]?fi[flag]-di[flag]:0;
        }
            
        return list;
    }
}

 

posted @ 2017-09-28 21:57  轻红  阅读(320)  评论(0编辑  收藏  举报