-
Notifications
You must be signed in to change notification settings - Fork 18
/
RightArea.java
66 lines (52 loc) · 2.22 KB
/
RightArea.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
====================================================
Desafios Intermediários - GFT Start #4 Java
====================================================
1/3 - Área Direita
====================================================
Desafio
Leia um caractere maiúsculo, que indica uma operação que deve ser realizada e uma matriz M[12][12]. Em seguida, calcule e mostre a soma ou a média considerando somente aqueles elementos que estão na área direita da matriz, conforme ilustrado abaixo (área verde).
{area_direita.png}
Entrada
A primeira linha de entrada contem um único caractere Maiúsculo O ('S' ou 'M'), indicando a operação (Soma ou Média) que deverá ser realizada com os elementos da matriz. Seguem os 144 valores de ponto flutuante que compõem a matriz.
Saída
Imprima o resultado solicitado (a soma ou média), com 1 casa após o ponto decimal.
-----------------------------------------
| Exemplo de Entrada | Exemplo de Saída |
-----------------------------------------
| S | 111.4 |
| 1.0 | |
| 330.0 | |
| -3.5 | |
| 2.5 | |
| 4.1 | |
| ... | |
-----------------------------------------
SOLUÇÃO ABAIXO: */
import java.io.IOException;
import java.util.Scanner;
public class RightArea {
public static void main(String[] args) throws IOException {
Scanner leitor = new Scanner(System.in);
while (leitor.hasNext()) {
String operation = leitor.next();
int rightArea = 0, patternSize = 12;
double sum = 0d;
double pattern[][] = new double[patternSize][patternSize];
for (int i=0; i<patternSize;i++)
for (int j=0; j<patternSize;j++) pattern[i][j] = leitor.nextDouble();
for (int k=0; k<patternSize;k++) {
for (int l=0; l<patternSize;l++) {
int aux = patternSize-1;
if (l>k && l>(aux-k) && l>(aux/2)) {
sum += pattern[k][l];
rightArea++;
}
}
}
double result = (operation.equals("S")) ? sum : sum/(double)rightArea;
System.out.printf("%.1f\n",result);
}
leitor.close();
}
}