Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
哪位帮忙看看,java写的In Reply To:又是TLE。。。。。。郁闷哪!!! Posted by:jucai at 2007-04-06 20:05:43 import java.util.*; class Bonds { int num; int in; Bonds(int n,int i){ num = n; in = i; } } class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int cs = cin.nextInt(); while (cs != 0) { int capital = cin.nextInt(); int years = cin.nextInt(); int cbonds = cin.nextInt(); Bonds bonds [] = new Bonds[cbonds]; for(int i = 0 ; i < cbonds; i ++) bonds[i] = new Bonds(cin.nextInt(),cin.nextInt()); int result = endCapital(capital,years,cbonds,bonds); System.out.println(result); cs --; } } public static int endCapital(int capital,int years,int cbonds,Bonds [] bonds){ if( years == 0) return capital; else { int [][]result = new int[cbonds][10000] ; for (int i = 0;i <= capital/bonds[0].num ; i ++ ){ result[0][i] = i*bonds[0].in; } for(int i = 1; i < cbonds; i ++) for(int j = 0; j <= capital/bonds[i].num; j ++){ int sum1 = 0; for(int k = 0; k <= capital/bonds[i-1].num; k ++){ int sum2 = 0; if(j*bonds[i].num+k*bonds[i-1].num <= capital) sum2 = j*bonds[i].in+result[i-1][k]; if(j*bonds[i].num+k*bonds[i-1].num > capital) break; if(sum2 > sum1) sum1 = sum2; } result[i][j] = sum1; } int max = -1; for(int i = 0; i < capital/bonds[cbonds-1].num; i ++) if(result[cbonds-1][i] > max) max = result[cbonds-1][i]; return endCapital(capital+max,years -1,cbonds,bonds); } } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator