Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1216
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1216
Getline One
By Neilor Tonin, URI Brazil
Timelimit: 1
Mangojata is learning Computer Programming. She finds everything very easy, very simple. She is going to make a small program that reads the names of your friends and the distance from his home to each of them. Therefore, she simply want to calculate what is the average distance between her house and her friends house (in meters). But Aristoclenes, a much more experienced programmer warned her that sometimes what seems simple can have some details, depending on the programming language used for implementation.
Input
The input contains several test cases. Each test case is composed by two lines. The first line contains the name of a friend of Mangojata. The second line contains an integer number that indicates the average distance between Mangojata's house and the house of a friend.
Output
The output must be a number with one digit after the decimal point (please use a double precision variable) indicating the average distance between Mangojata's house and the house of her friends, like the following example.
Sample Input | Sample Output |
Juca Pivara |
160.7 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main (void)
{
char nome[50];
unsigned short contador = 0;
double distancia, soma = 0;
while (scanf(" %[^\n] %lf", nome, &distancia) != EOF)
{
soma += distancia;
contador++;
}
printf("%.1lf\n", soma/contador);
}
Copy The Code &
Try With Live Editor
Input
410
Pedro Medario
12
Marta Mandua
60
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
int q = 0;
string lx;
double n, s;
while (getline(cin, lx)) {
cin >> n;
getchar();
++q;
s += n;
}
cout << fixed << setprecision(1);
cout << double(s / q) << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
410
Pedro Medario
12
Marta Mandua
60
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static FastScanner in = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
String line;
double c = 0d;
double avg = 0d;
while ((line = in.nextLine()) != null) {
avg += in.nextDouble();
c++;
}
out.printf("%.1f\n", avg / c);
in.close();
out.close();
}
static class FastScanner implements Closeable {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public FastScanner(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
public String next() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
public String nextLine() throws IOException {
tokenizer = null;
return reader.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public String[] nextStringArray() throws IOException {
return nextLine().split("\\s");
}
public int[] nextIntArray() throws IOException {
String[] stringArray = nextStringArray();
int length = stringArray.length;
int[] array = new int[length];
for (int i = 0; i < stringArray.length; i++) {
array[i] = Integer.parseInt(stringArray[i]);
}
return array;
}
public long[] nextLongArray() throws IOException {
String[] stringArray = nextStringArray();
int length = stringArray.length;
long[] array = new long[length];
for (int i = 0; i < stringArray.length; i++) {
array[i] = Long.parseLong(stringArray[i]);
}
return array;
}
public float[] nextFloatArray() throws IOException {
String[] stringArray = nextStringArray();
int length = stringArray.length;
float[] array = new float[length];
for (int i = 0; i < stringArray.length; i++) {
array[i] = Float.parseFloat(stringArray[i]);
}
return array;
}
public double[] nextDoubleArray() throws IOException {
String[] stringArray = nextStringArray();
int length = stringArray.length;
double[] array = new double[length];
for (int i = 0; i < stringArray.length; i++) {
array[i] = Double.parseDouble(stringArray[i]);
}
return array;
}
@Override
public void close() throws IOException {
tokenizer = null;
reader.close();
}
}
}
Copy The Code &
Try With Live Editor
Input
410
Pedro Medario
12
Marta Mandua
60
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
const arr = [];
for (let i=1;lines[i];i+=2){
const t = lines[i]
arr.push(t)
}
console.log(
(arr.reduce((acc, curr)=> acc + Number(curr), 0)/ arr.length).toFixed(1)
)
Copy The Code &
Try With Live Editor
Input
410
Pedro Medario
12
Marta Mandua
60
Output
#5 Code Example with Python Programming
Code -
Python Programming
n = 0
i = 0
while True:
try:
e = input()
n += int(input())
i += 1
except EOFError:
print('{:.1f}'.format(float(n / i)))
break
Copy The Code &
Try With Live Editor
Input
410
Pedro Medario
12
Marta Mandua
60
Output