三个for循环,注意判断边界,然后就过了。。但是第二题怎么做都超时,希望有人贴一下解法!
import java.util.*;
public class Main{
public void process(){
Scanner sc = new Scanner(System.in);
String pen;
String box;
String price;
String[] penStArr;
String[] boxStArr;
String[] priceStArr;
int colorPen;
int whitePen;
int colorA;
int whiteB;
int whiteC;
int colorD;
int x;
int y;
int z;
int sum1;
int sum2;
int sum3;
while(sc.hasNext()){
pen = sc.nextLine();
box = sc.nextLine();
price = sc.nextLine();
colorPen = Integer.parseInt(pen.split(" ")[0]);
whitePen = Integer.parseInt(pen.split(" ")[1]);
colorA = Integer.parseInt(box.split(" ")[0]);
whiteB = Integer.parseInt(box.split(" ")[1]);
whiteC = Integer.parseInt(box.split(" ")[2]);
colorD = Integer.parseInt(box.split(" ")[3]);
x = Integer.parseInt(price.split(" ")[0]);
y = Integer.parseInt(price.split(" ")[1]);
z = Integer.parseInt(price.split(" ")[2]);
int bound1 = colorPen/colorD;
int bound2 = whitePen/whiteC;
int bound3 = (colorPen/colorA > whitePen/whiteB)? whitePen/whiteB : colorPen/colorA;
int finalSum = 0;
int tempSum = 0;
for(int i = 0; i<= bound1; i++){
for(int j = 0; j<= bound2; j++){
for(int m = 0; m<= bound3; m++){
if((i*colorD + m*colorA) > colorPen || (j*whiteC+m*whiteB) > whitePen){
break;
}
tempSum = i*z + j*y + m*x;
finalSum = tempSum > finalSum ? tempSum : finalSum;
}
}
}
System.out.println(finalSum);
}
}
public static void main(String[] args){
Main m = new Main();
m.process();
}
}