718.实验
题目描述
医学部一共进行了N场动物实验。
共有三种小动物可用来实验,分别是青蛙、老鼠和兔子。
每次实验都会选取其中一种动物来参与实验,选取数量若干。
现在请你统计一下医学部一共用了多少小动物,每种分别用了多少,每种动物使用数量占总量的百分比分别是多少。
输入格式
第一行包含整数N,表示实验次数。
接下来N行,每行包含一个整数A(表示一次实验使用的小动物的数量)和一个字符T(表示一次实验使用的小动物的类型,’C’表示兔子(coney),’R’表示老鼠(rat),’F’表示青蛙(frog))。
输出格式
请你参照输出样例,输出所用动物总数,每种动物的数量,以及每种动物所占百分比。
注意输出百分比时,保留两位小数。
数据范围
1≤N≤100
1≤A≤15
样例
输入样例:
10
10 C
6 R
15 F
5 C
14 R
9 C
6 R
8 F
5 C
14 R
输出样例:
Total: 92 animals
Total coneys: 29
Total rats: 40
Total frogs: 23
Percentage of coneys: 31.52 %
Percentage of rats: 43.48 %
Percentage of frogs: 25.00 %
算法
(暴力循环)
无法描述的基础题
时间复杂度 $O(n)$
参考文献
无
C++ 代码
#include <iostream>
using namespace std;
int n,t,a[105];
char a1[105];
double tot,c,r,f,c1,r1,f1;
int main()
{
cin >> n;
t = n;
while(t--){
cin >> a[t] >> a1[t];
}
for(int i = 0;i < n;i++){
if(a1[i] == 'C'){
c += a[i];
tot += a[i];
}
else
{
if(a1[i] == 'R'){
r += a[i];
tot += a[i];
}
else
{
f += a[i];
tot += a[i];
}
}
}
c1 = c / tot * 100;
r1 = r / tot * 100;
f1 = f / tot * 100;
printf("Total: %.0lf animals\n",tot);
printf("Total coneys: %.0lf\n",c);
printf("Total rats: %.0lf\n",r);
printf("Total frogs: %.0lf\n",f);
printf("Percentage of coneys: %.2lf %\n",c1);
printf("Percentage of rats: %.2lf %\n",r1);
printf("Percentage of frogs: %.2lf %\n",f1);
return 0;
}