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 |
Re:哪位帮忙看看,java写的In Reply To:哪位帮忙看看,java写的 Posted by:jucai at 2007-04-06 20:06:53 > 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); > } > } > } 不需要那么麻烦吧 package _2014_02_19; import java.util.*; public class Demo_5 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sca = new Scanner(System.in); int no=sca.nextInt(); while (no>0) {no--; long sum = sca.nextLong(); int year = sca.nextInt(); int num = sca.nextInt(); int ai[] = new int[num + 1]; int bi[] = new int[num + 1]; for (int i = 1; i <= num; i++) { ai[i] = sca.nextInt()/1000; bi[i] = sca.nextInt(); } while (year > 0) { //System.out.println(sum); long kk=sum/1000; int ci[]=new int[(int) kk+1]; for (int i = 1; i <= num; i++) { for (int j = 0; j <= kk; j++) { if (j - ai[i] >=0) { if(ci[j]<ci[j-ai[i]]+bi[i]){ ci[j]=ci[j-ai[i]]+bi[i]; } } } }int max=ci[0]; for (int j = 1; j <= kk; j++) { if(max<ci[j]){ max=ci[j]; } }sum=sum+max; year--; }System.out.println(sum); } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator