第一种写法
#include <cstdio>
#include <iostream>
using namespace std;
int C, R, F;
int total;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int a;
char b;
cin >> a >> b;
if (b == 'C') C += a;
if (b == 'R') R += a;
if (b == 'F') F += a;
}
total = C + R + F;
printf("Total: %d animals\n", total);
printf("Total coneys: %d\n", C);
printf("Total rats: %d\n", R);
printf("Total frogs: %d\n", F);
printf("Percentage of coneys: %.2lf %%\n", C * 1.0 / total * 100);
printf("Percentage of rats: %.2lf %%\n", R * 1.0 / total * 100);
printf("Percentage of frogs: %.2lf %%\n", F * 1.0 / total * 100);
return 0;
}
第二种写法
#include <cstdio>
#include <iostream>
using namespace std;
int C, R, F;
int total;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int a;
char b[5];
scanf("%d%s", &a, b);
if (b[0] == 'C') C += a;
if (b[0] == 'R') R += a;
if (b[0] == 'F') F += a;
}
total = C + R + F;
printf("Total: %d animals\n", total);
printf("Total coneys: %d\n", C);
printf("Total rats: %d\n", R);
printf("Total frogs: %d\n", F);
printf("Percentage of coneys: %.2lf %%\n", C * 1.0 / total * 100);
printf("Percentage of rats: %.2lf %%\n", R * 1.0 / total * 100);
printf("Percentage of frogs: %.2lf %%\n", F * 1.0 / total * 100);
return 0;
}