URI Online Judge Solution 1187 Top Area Using C, Python Programming Language.
Read an uppercase character that indicates an operation that will be performed in an array M[12][12]. Then, calculate and print the sum or average considering only that numbers that are included in the green area of this array, like shown in the following figure.
Input
The first line of the input contains a single uppercase character O ('S' or 'M'), indicating the operation Sum or Average (Média in portuguese) to be performed with the elements of the array. Follow 144 double numbers of the array.
Output
Print the calculated result (sum or average), with one digit after the decimal point.
Input Sample | Output Sample |
S | 126.2 |
Solution Using C:
#include <stdio.h>
int main()
{
double a=0.0, M[12][12];
char T[2];
int x,y,c=0;
scanf("%s", &T);
for(x=0;x<=11;x++)
{
for(y=0; y<=11; y++)
{
scanf("%lf", &M[x][y]);
}
}
for(x=0; x<5; x++)
{
for(y=x+1; y<11-x; y++)
a += M[x][y];
}
if(T[0]=='S')
printf("%.1lf\n",a);
else if(T[0]=='M')
{
a=a/30.0;
printf("%.1lf\n",a);
}
return 0;
}
Comments
Post a Comment