(URI) Linha na Matriz - Solução
import java.io.*;
import java.util.*;
class Main {
static final int NUM = 12;
public static void readMatrix(Scanner sc, float[][] matrix) {
for (int i = 0; i < NUM; i++) {
for (int j = 0; j < NUM; j++) {
matrix[i][j] = sc.nextFloat();
}
}
}
public static float calcSum(int line, float[][] matrix) {
float sum = 0;
for (int i = 0; i < NUM; i++) {
sum += matrix[line][i];
}
return sum;
}
public static void printAnswer(String s, float sum) {
if (s.equals("S")) {
System.out.println(String.format("%.1f", sum));
}
else {
System.out.println(String.format("%.1f", sum/NUM));
}
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
float[][] matrix = new float[NUM][NUM];
int line = sc.nextInt();
String s = sc.next();
readMatrix(sc, matrix);
float sum = calcSum(line, matrix);
printAnswer(s, sum);
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.util.*;
class Main {
static final int NUM = 12;
public static void readMatrix(Scanner sc, float[][] matrix) {
for (int i = 0; i < NUM; i++) {
for (int j = 0; j < NUM; j++) {
matrix[i][j] = sc.nextFloat();
}
}
}
public static float calcSum(int line, float[][] matrix) {
float sum = 0;
for (int i = 0; i < NUM; i++) {
sum += matrix[line][i];
}
return sum;
}
public static void printAnswer(String s, float sum) {
if (s.equals("S")) {
System.out.println(String.format("%.1f", sum));
}
else {
System.out.println(String.format("%.1f", sum/NUM));
}
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
float[][] matrix = new float[NUM][NUM];
int line = sc.nextInt();
String s = sc.next();
readMatrix(sc, matrix);
float sum = calcSum(line, matrix);
printAnswer(s, sum);
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment