I tried a quiz site called Code Nachara. Below is the answer I made at that time. I got stuck in the hasNext () method in the Scanner class. When executing standard input as a scan target, the hasNext () function does not return false unless the input is properly terminated with Ctrl + z at the end of the standard input. Therefore, if you do not finish it properly, you will not get out of the loop.
It's been a long time without noticing this ...
that's all
package questions;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int count = 1;
int member = 0;
String input = null;
double resultAvgs[] = new double[12];
try{
Scanner scanner = new Scanner(System.in);
// resultAvgs[]Store in
while(scanner.hasNext()){
input = scanner.nextLine();
resultAvgs[member] = calcAvrg(input);
member += 1;
}
for(int i=0; i<member; i++){
for(int j=0; j<member; j++){
if(i==j){
continue;
}else{
if(resultAvgs[i] >= resultAvgs[j]){
//If there is a small value, drop one rank
count += 1;
}
}
}
System.out.println(count);
//Initialization
count = 1;
}
scanner.close();
}catch(NullPointerException e){
e.printStackTrace();
}
}
//Read the race result and calculate the harmonic mean
static double calcAvrg(String in) {
double total = 0;
String[] results = in.split(" ", 0);
for (String result : results) {
double buff = 1 / Double.parseDouble(result);
total += buff;
}
return results.length / total;
}
}
Recommended Posts