(URI) Acima da Diagonal Secundária - Solução
import java.io.*;
import java.util.*;
class Main {
static final int NUM = 12;
static int total;
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(float[][] matrix) {
float sum = 0;
int limit = NUM;
total = 0;
for (int i = 0; i < NUM; i++) {
limit--;
for (int j = 0; j < limit; j++) {
sum += matrix[i][j];
total++;
}
}
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/total));
}
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
float[][] matrix = new float[NUM][NUM];
String s = sc.next();
readMatrix(sc, matrix);
float sum = calcSum(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;
static int total;
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(float[][] matrix) {
float sum = 0;
int limit = NUM;
total = 0;
for (int i = 0; i < NUM; i++) {
limit--;
for (int j = 0; j < limit; j++) {
sum += matrix[i][j];
total++;
}
}
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/total));
}
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
float[][] matrix = new float[NUM][NUM];
String s = sc.next();
readMatrix(sc, matrix);
float sum = calcSum(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