Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1808
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1808
The Return of The King
By Pablo Ariel Heiber, Universidad de Buenos Aires Argentina
Timelimit: 1
The prolific author Stephen King was entering the grades of his literature students in an on-line general average calculator. When he finished, he noticed his return key was broken so instead of entering the grades of a student in a separate line each, he entered them in a single line without any separation. Since Mr. King does not have the skills to fix his return key right away, he needs you to calculate the average of the grades of the student from the non-separated input.
Each grade is an integer between 1 and 10. All grades were entered written in base 10 without leading zeros. For example, if the grades of Mr. King’s student were 3, 10, 1 and 10 they would be entered as “310110”.
Input
The input consists of a single line that contains a non-empty string S of at most 100 base 10 digits. There is a unique way to partition S into a list of substrings such that each substring represents an integer between 1 and 10 in base 10 without leading zeros.
Output
Output a line with a rational number representing the average of the grades of the student whose grades Mr. King entered as S. The result must be output as a rational number with exactly two digits after the decimal point, rounded if necessary.
Input Samples | Output Samples |
310110 |
6.00 |
10910 |
9.67 |
222222223 |
2.11 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
int cpn(char c)
{
switch (c)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
}
}
int main(void)
{
int i = 0, qnt = 0, soma = 0;
char notas[101];
scanf("%s", notas);
for (i = 0; i < strlen(notas); ++i)
{
if (notas[i] == '1' && notas[i + 1] == '0')
{
soma += 10;
++i;
}
else
soma += cpn(notas[i]);
qnt++;
}
printf("%.2lf\n", (double)(soma / (double)(qnt * 1.0)));
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
short cpn(char c) {
return c - 48;
}
int main(void)
{
int i, qnt = 0;
double soma = 0;
char notas[101];
cin >> notas;
for (i = 0; i < strlen(notas); ++i) {
if (notas[i] == '1' and notas[i+1] == '0') {
soma += 10;
++i;
}
else soma += cpn(notas[i]);
++qnt;
}
cout << fixed << setprecision(2);
cout << soma/qnt << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
e = str(input())
s = 0
q = 0
while len(e) > 0:
if e[-1] != '0':
s += int(e[-1])
e = e[:len(e)-1]
else:
s += 10
e = e[:len(e)-2]
q += 1
print('{:.2f}'.format(s / q))
Copy The Code &
Try With Live Editor
Input
Output