Repository navigation
Expand file tree
/
Copy pathQ14.java
More file actions
101 lines (77 loc) · 2.74 KB
/
Copy pathQ14.java
File metadata and controls
101 lines (77 loc) · 2.74 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Locale;
import java.util.Scanner;
public class Q14 {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
try (Scanner sc = new Scanner(System.in)) {
int l = sc.nextInt();
int c = sc.nextInt();
int[][] matriz = lerMatriz(sc, l, c);
double media = calcularMedia(matriz);
System.out.println("linha com maior soma: " + linhaMaiorSoma(matriz));
System.out.println("coluna com menor soma: " + colunaMenorSoma(matriz));
System.out.printf("media geral: %.2f%n", media);
System.out.println("quantidade acima da media: " + quanntidadeAcimaDaMedia(matriz, media));
}
}
public static int[][] lerMatriz(Scanner sc, int l, int c) {
int[][] matriz = new int[l][c];
for (int i = 0; i < l; i++) {
for (int j = 0; j < c; j++) {
matriz[i][j] = sc.nextInt();
}
}
return matriz;
}
public static int linhaMaiorSoma(int[][] matriz) {
int maiorSoma = Integer.MIN_VALUE;
int indiceLinha = 0;
for (int i = 0; i < matriz.length; i++) {
int soma = 0;
for (int j = 0; j < matriz[i].length; j++) {
soma += matriz[i][j];
}
if (soma > maiorSoma) {
maiorSoma = soma;
indiceLinha = i;
}
}
return indiceLinha;
}
public static int colunaMenorSoma(int[][] matriz) {
int menorSoma = Integer.MAX_VALUE;
int indiceColuna = 0;
for (int j = 0; j < matriz[0].length; j++) {
int soma = 0;
for (int i = 0; i < matriz.length; i++) {
soma += matriz[i][j];
}
if (soma < menorSoma) {
menorSoma = soma;
indiceColuna = j;
}
}
return indiceColuna;
}
public static double calcularMedia(int[][] matriz) {
int somaTotal = 0;
int total = matriz.length * matriz[0].length;
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[i].length; j++) {
somaTotal += matriz[i][j];
}
}
return (double) somaTotal / total;
}
public static int quantidadeAcimaDaMedia(int[][] matriz, double media) {
int contador = 0;
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[i].length; j++) {
if (matriz[i][j] > media) {
contador++;
}
}
}
return contador;
}
}