Migliore risposta
In java, puoi usare la classe Scanner per leggere linput e System.out per stampare loutput.
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // read an integer from input stream.
System.out.println(n) // print n
Tuttavia, a volte ottieni Time Limit Exceeded (TLE) a causa delle prestazioni di Scanner. Puoi invece utilizzare BufferedReader.
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
Risposta
Vorrei provare a spiegare con Angry Professor problema da HackerRank.
Dopo la lettura del codice da System.in e la stampa delloutput, il resto si spiega da sé:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int nstudent = in.nextInt();
int threshold = in.nextInt();
int[] arrival = new int[nstudent];
for (int j = 0; j < nstudent; j++) {
arrival[j] = in.nextInt();
}
algo(arrival, threshold);
}
}
public static void algo(int[] arrival, int threshold){
//your implementation
}
}