Gone Fishing
阿新 • • 發佈:2017-09-29
.org public 找到 blog turn 越界 span pri 問題
原題網址
代碼已經寫出來了,自己測試的時候沒有問題,提交上去之後反饋了我一個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(inti=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; } }
Gone Fishing