AcWing 718. 实验
原题链接
简单
作者:
最后的轻语_8
,
2025-01-16 20:51:08
,
所有人可见
,
阅读 2
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
//scanf 在输入的时候
int n = 0;
cin >> n;
int c = 0 ,r = 0,f = 0;
for(int i = 0; i < n ; i ++)
{
int k = 0;
char t;
cin >> k >> t;
//scanf("%d %c",&k,&t);
// 这样子写不可以,因为scanf读入一个字符的时候 不会自动过滤掉空格、回车和制表符(tab)
if( t == 'C') c += k;
else if(t == 'R') r += k;
else f += k;
}
int s = c + r + f;
cout << "Total: " << s << " animals" << endl;
cout << "Total coneys: " << c << endl;
cout << "Total rats: " << r << endl;
cout << "Total frogs: " << f << endl;
// cout << "Percentage of coneys: " << c / n << "%" << endl;
//此处算出是整形的 ,要转换成浮点数
//cout << "Percentage of rats: " << r / n << "%" << endl;
//cout << "Percentage of frogs: " << f / n << "%" << endl;
printf("Percentage of coneys: %.2lf %%\n",(double)c / s * 100);
// 1.0 * c / s *100 这样也可以
// 此处保留2位小数且 将结果转换成浮点数
printf("Percentage of rats: %.2lf %%\n",(double)r / s * 100);
printf("Percentage of frogs: %.2lf %%\n",(double)f / s * 100);
return 0;
}