(UVA) My Dear Neighbours - Solution
import java.io.*;
import java.util.*;
class Main {
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void printAnswer(int numNeighbors, int[] neighbors, int better) {
boolean firstTime = true;
for (int j = 1; j <= numNeighbors; j++) {
if (neighbors[j] == better) {
if (!firstTime) {
System.out.print(" ");
}
System.out.print(j);
firstTime = false;
}
}
System.out.println();
}
public static int findBetterNeighbor(int numNeighbors, int[] neighbors) {
int better = 1000000000;
for (int j = 1; j <= numNeighbors; j++) {
if (neighbors[j] < better) {
better = neighbors[j];
}
}
return better;
}
public static void readNeighbors(BufferedReader br, int numNeighbors, int[] neighbors) throws NumberFormatException, IOException {
for (int j = 1; j <= numNeighbors; j++) {
String line = br.readLine();
String[] num = line.split(" ");
neighbors[j] = num.length;
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = reader(br);
for (int i = 0; i < numCases; i++) {
int numNeighbors = reader(br);
int[] neighbors = new int[numNeighbors+1];
readNeighbors(br, numNeighbors, neighbors);
int better = findBetterNeighbor(numNeighbors, neighbors);
printAnswer(numNeighbors, neighbors, better);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.util.*;
class Main {
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void printAnswer(int numNeighbors, int[] neighbors, int better) {
boolean firstTime = true;
for (int j = 1; j <= numNeighbors; j++) {
if (neighbors[j] == better) {
if (!firstTime) {
System.out.print(" ");
}
System.out.print(j);
firstTime = false;
}
}
System.out.println();
}
public static int findBetterNeighbor(int numNeighbors, int[] neighbors) {
int better = 1000000000;
for (int j = 1; j <= numNeighbors; j++) {
if (neighbors[j] < better) {
better = neighbors[j];
}
}
return better;
}
public static void readNeighbors(BufferedReader br, int numNeighbors, int[] neighbors) throws NumberFormatException, IOException {
for (int j = 1; j <= numNeighbors; j++) {
String line = br.readLine();
String[] num = line.split(" ");
neighbors[j] = num.length;
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = reader(br);
for (int i = 0; i < numCases; i++) {
int numNeighbors = reader(br);
int[] neighbors = new int[numNeighbors+1];
readNeighbors(br, numNeighbors, neighbors);
int better = findBetterNeighbor(numNeighbors, neighbors);
printAnswer(numNeighbors, neighbors, better);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment