实验
医学部一共进行了N场动物实验。
共有三种小动物可用来实验,分别是青蛙、老鼠和兔子。
每次实验都会选取其中一种动物来参与实验,选取数量若干。
现在请你统计一下医学部一共用了多少小动物,每种分别用了多少,每种动物使用数量占总量的百分比分别是多少。
输入格式
第一行包含整数N,表示实验次数。
接下来N行,每行包含一个整数A(表示一次实验使用的小动物的数量)和一个字符T(表示一次实验使用的小动物的类型,’C’表示兔子(coney),
’R’表示老鼠(rat),
’F’表示青蛙(frog))。
输出格式
请你参照输出样例,输出所用动物总数,每种动物的数量,以及每种动物所占百分比。
注意输出百分比时,保留两位小数。
数据范围
1≤N≤100
1≤N≤100
,
1≤A≤15
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class experiment{
public:
experiment(int ti=0,char ty='0'):times(ti),type(ty){} //constructor
friend istream &operator >>(istream &,experiment &); //重载>>输入数据
void typeCount(); //分类计数函数
void timesTotal(); //求总数函数
static double percentage(int); //求占比函数
static int total; //静态 total 储存总数
static int t[3]; //静态数组储存每类数量
private:
int times;
char type;
};
int experiment::t[3] = {0,0,0}; //初始化静态数据
int experiment::total = 0;
void experiment::typeCount(){
if(type == 'C') t[0] += times;
if(type == 'R') t[1] += times;
if(type == 'F') t[2] += times;
}
double experiment::percentage(int i){
return (double)t[i] / total;
}
void experiment::timesTotal(){
total = t[0] + t[1] + t[2];
}
istream &operator >>(istream & input,experiment & a){
input>>a.times>>a.type;
return input;
}
int main(){
string b[3] = {"coneys","rats","frogs"};
int n;
experiment a[100];
cin>>n;
for(int i = 0;i < n;i++){
cin>>a[i];
a[i].typeCount(); //每输入一组数据就开始计数
a[i].timesTotal();
}
cout<<"Total: "<< experiment::total<<" animals"<<endl;
for(int i = 0 ; i < 3 ; i++)
cout<<"Total "<< b[i] <<": " <<experiment::t[i]<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2);
for(int i = 0 ; i < 3 ; i++)
cout<<"Percentage of "<< b[i] <<": "<<experiment::percentage(i)*100<<" %"<<endl;
return 0;
}