[URI Online Judge] – 1094 – Experiments

Maria has just started as graduate student in a medical school and she’s needing your help to organize a laboratory experiment which she is responsible about. She wants to know, at the end of the year, how many animals were used in this laboratory and the percentage of each type of animal is used at all.

This laboratory uses in particular three types of animals: frogs, rats and rabbits. To obtain this information, it knows exactly the number of experiments that were performed, the type and quantity of each animal is used in each experiment.


The first line of input contains an integer N indicating the number of test cases that follows. Each test case contains an integer Amount (1 ≤ Amount ≤ 15) which represents the amount of animal used and a character Type (‘C‘, ‘R‘ or ‘S‘), indicating the type of animal:
C: Coelho (rabbit in portuguese)
R: Rato (rat  in portuguese)
S: Sapo (frog in portuguese)


Print the total of animals used, the total of each type of animal and the percentual of each one in relation of the total of animals used. The percentual must be printed with 2 digits after the decimal point.

Sample Input Sample Output
10 C
6 R
15 S
5 C
14 R
9 C
6 R
8 S
5 C
14 R
Total: 92 cobaias
Total de coelhos: 29
Total de ratos: 40
Total de sapos: 23
Percentual de coelhos: 31.52 %
Percentual de ratos: 43.48 %
Percentual de sapos: 25.00 %

My solution:

#include <cstdio>
int main(){
 char t;
 int n, c = 0, r = 0, s = 0, soma = 0, e;
scanf("%d", &n);
 scanf("%d %c", &e, &t);
 soma += e;
 if (t == 'C'){
 c += e;
 } else if (t == 'R'){
 r += e;
 } else if (t == 'S'){
 s += e;
printf("Total: %d cobaias\n", soma);
 printf("Total de coelhos: %d\n", c);
 printf("Total de ratos: %d\n", r);
 printf("Total de sapos: %d\n", s);
 printf("Percentual de coelhos: %.2f %\n", (float)c * 100.0 / soma);
 printf("Percentual de ratos: %.2f %\n", (float)r * 100.0 / soma);
 printf("Percentual de sapos: %.2f %\n", (float)s * 100.0 / soma);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s