【发布时间】:2015-06-05 00:50:20
【问题描述】:
请参考这个UVa OJ问题:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2595
它适用于单个测试用例。但是,对于测试用例的数量没有给定的输入,这意味着程序应该只通过读取问题变量的输入来知道何时继续以及何时停止
我正在考虑使用scanner.hasNextLine()方法作为条件;如果是真的,请重新开始 main ,但我不知道该怎么做。有什么线索吗?
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
int N = scanner.nextInt();
int B = scanner.nextInt();
int H = scanner.nextInt();
int W = scanner.nextInt();
int [] priceArray = new int [H];
int [] availableBeds = new int [W];
int cheapestStay = 999999999;
for (int i=0; i<H; i++){
priceArray[i] = scanner.nextInt();
for (int j=0; j<W; j++){
availableBeds[j] = scanner.nextInt();
if (availableBeds[j] >= N && priceArray[i]*N <= B && priceArray[i]*N < cheapestStay){
cheapestStay = priceArray[i]*N;
}
}
}
if (cheapestStay != 999999999){
System.out.println(cheapestStay);
}else{
System.out.println("stay home");
}
/*if (!scanner.hasNextLine)
repeat*/
}
【问题讨论】:
-
将您的业务逻辑代码包装在一个单独的方法中,然后根据条件一次又一次地调用该方法在
main(..)方法中调用该方法。