AcWing 718. 实验 - Java
原题链接
困难
作者:
KYCygni
,
2021-03-13 09:25:43
,
所有人可见
,
阅读 354
Java 代码
import java.util.Scanner;
import java.util.HashMap;
public class Main
{
static final String CONEYS = "coneys";
static final String RATS = "rats";
static final String FROGS = "frogs";
private static void add(HashMap map, String key, int value)
{
int x = (Integer)map.get(key);
map.put(key, value + x);
}
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
HashMap anm = new HashMap();
anm.put(CONEYS, 0);
anm.put(RATS, 0);
anm.put(FROGS, 0);
for (int i = 0; i < n; i ++)
{
int x = cin.nextInt();
char m = cin.next().charAt(0);
if ( m == 'C' )
add (anm, CONEYS, x);
else if (m == 'R')
add (anm, RATS, x);
else if (m == 'F')
add (anm, FROGS, x);
}
n = (Integer)anm.get(CONEYS) + (Integer)anm.get(RATS) + (Integer)anm.get(FROGS);
System.out.println("Total: " + n + " animals");
System.out.println("Total " + CONEYS + ": " + anm.get(CONEYS));
System.out.println("Total " + RATS + ": " + anm.get(RATS));
System.out.println("Total " + FROGS + ": " + anm.get(FROGS));
System.out.println("Percentage of " + CONEYS + ": " + String.format("%.2f", 100.0*(Integer)anm.get(CONEYS)/n) + " %");
System.out.println("Percentage of " + RATS + ": " + String.format("%.2f", 100.0*(Integer)anm.get(RATS)/n) + " %" );
System.out.println("Percentage of " + FROGS + ": " + String.format("%.2f", 100.0*(Integer)anm.get(FROGS)/n) + " %" );
}
}