AcWing 718. 实验-看这篇就够了
原题链接
简单
作者:
枫哥
,
2024-10-23 11:26:49
,
所有人可见
,
阅读 2
#include <iostream>
#include <cstdio>
using namespace std;
int N,A,total,coneys,rats,frogs;
//int N,A,total,coneys = rats = frogs = 0;
// 这样写包错,因为frogs对于rats来说未声明,C++不允许用未声明变量赋值
// rats对于coneys同理
char T;
int main(){
total=coneys=rats=frogs=0; // 0赋值给frogs,frogs赋值给rats...
cin >> N;
while(cin>>A >>T && N--){
if (T == 'C') coneys += A;
else if (T == 'R') rats += A;
else frogs += A;
}
total = coneys+rats+frogs;
printf("Total: %d animals\n",total);
printf("Total coneys: %d\n",coneys);
printf("Total rats: %d\n",rats);
printf("Total frogs: %d\n",frogs);
printf("Percentage of coneys: %.2f %%\n",(float)coneys/total*100);
// 处理整数除法四舍五入问题,转浮点数(强制转换或3.0)
// 由于求百分数故*100
printf("Percentage of rats: %.2f %%\n",(float)rats/total*100);
printf("Percentage of frogs: %.2f %%\n",(float)frogs/total*100);
return 0;
}