Algorithm
Problem Name: beecrowd | 1187
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1187
Top Area
By Neilor Tonin, URI Brazil
Timelimit: 1
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 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c;
cin >> c;
double sum = 0;
double a[12][12];
for(int i = 0 ; i < 12; i++){
for(int j = 0 ; j < 12 ; j++){
cin >> a[i][j];
if(j > i && (i+j) < 11) sum += a[i][j];
}
}
if(c == 'S') printf("%.1lf\n" , sum);
else if(c == 'M') printf("%.1lf\n" , sum/30>;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var operation = lines.shift();
var positions = [];
var value = 0;
for(var i = 0; i < 5; i++){
value = i * 12;
for(var j = i+1; j < 11-i; j++){
positions.push(value + j);
}
}
var sum = 0;
var count = 0;
for(var i = 0; i < 144; i++){
var num = parseFloat(lines.shift());
if(positions.indexOf(i)!=-1){
sum += num;
count++;
}
}
if(operation=='S'){
console.log(sum.toFixed(1));
}else{
console.log((sum/count).toFixed(1));
}
Copy The Code &
Try With Live Editor
Input
Output