URI Online Judge | 1181
Linha na Matriz
Por Neilor Tonin, URI
Brasil
Timelimit: 1
Neste problema você deve ler um número, indicando uma linha da matriz na qual uma operação deve ser realizada, um caractere maiúsculo, indicando a operação que será realizada, e todos os elementos de uma matriz M[12][12]. Em seguida, calcule e mostre a soma ou a média dos elementos que estão na área verde da matriz, conforme for o caso. A imagem abaixo ilustra o caso da entrada do valor 2 para a linha da matriz, demonstrando os elementos que deverão ser considerados na operação.

Entrada
A primeira linha de entrada contem um número L (0 ≤ L ≤ 11) indicando a linha que será considerada para operação. A segunda linha de entrada contém um único caractere Maiúsculo T ('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, sendo que a mesma é preenchida linha por linha, da linha 0 até a linha 11, sempre da esquerda para a direita.
Saída
Imprima o resultado solicitado (a soma ou média), com 1 casa após o ponto decimal.
URI Online Judge | 1181
Line in Array
By Neilor Tonin, URI
Brazil
Timelimit: 1
Your job in this problem is to read a number that is a line of an array, an uppercase character, indicating the operation to be performed and all elements of a bidimentional array M[12][12]. Then, you have to calculate and print the sum or average of all elements within the green area according to the case. The following figure illustrates the case when is entered the number 2 to the array line, showing all elements that must be considered in the operation.

Input
The first line of the input contains a simple integer L (0 ≤ L ≤ 11) indicating the line to be considered in the operation. The second line of the input contains a single uppercase character T ('S' or 'M'), indicating the operation Sum or Average (Média in portuguese) to be performed with the elements of the array. Follow the 144 floating-point numbers of the array, considering that the elements are inserted line by line, from line 0 to line 11, always from left to right.
Output
Print the calculated result (sum or average), with one digit after the decimal point.
#include <stdio.h>
int main()
{
double a=0.0, M[12][12];
char T[2];
int C,x,y;
scanf("%d", &C);
scanf("%s", &T);
for(x=0;x<=11;x++)
{
for(y=0; y<=11; y++)
{
scanf("%lf", &M[x][y]);
if(x==C)
a+=M[x][y];
}
}
if(T[0]=='S')
printf("%.1lf\n",a);
else if(T[0]=='M')
{
a=a/12.0;
printf("%.1lf\n",a);
}
return 0;
}
0 Comentários