题目描述
这个题会出精度问题,输出百分比时要先转换变量类型为double,否则输出0
还有输出%要用%%
样例
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n,a,A=0,ac=0,ar=0,af=0;
string t;
double x,y,z;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a>>t;
A+=a;
if(t=="C")ac+=a;
else if(t=="R")ar+=a;
else af+=a;
}
x=(double)ac*100/A;
y=(double)ar*100/A;
z=(double)af*100/A;
printf("Total: %d animals\nTotal coneys: %d\nTotal rats: %d\nTotal frogs: %d\nPercentage of coneys: %.2lf %%\nPercentage of rats: %.2lf %%\nPercentage of frogs: %.2lf %%",A,ac,ar,af,x,y,z);
return 0;
}