img
img


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum=0,sumc=0,sumr=0,sumf=0;
        for(int i=0;i<n;i++){
            int a=sc.nextInt();
            String t=sc.next();
            sum+=a;
            if("C".equals(t)) sumc+=a;
            else if("R".equals(t)) sumr+=a;
            else sumf+=a;
        }
        System.out.printf("Total: %d animals\n", sum);
        System.out.printf("Total coneys: %d\n", sumc);
        System.out.printf("Total rats: %d\n", sumr);
        System.out.printf("Total frogs: %d\n", sumf);
        System.out.printf("Percentage of coneys: %.2f %%\n", sumc * 100.0 / sum);
        System.out.printf("Percentage of rats: %.2f %%\n", sumr * 100.0 / sum);
        System.out.printf("Percentage of frogs: %.2f %%\n", sumf * 100.0 / sum);

    }
}

利用数组

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[26];
        for(int i=1;i<=n;i++)
        {
            int x=sc.nextInt();char c=sc.next().charAt(0);
            a[(int)(c-'A')]+=x;
        }
        int sum=a['F'-'A']+a['C'-'A']+a['R'-'A'];
        System.out.printf("Total: %d animals\n",sum);
        System.out.printf("Total coneys: %d\n",a['C'-'A']);
        System.out.printf("Total rats: %d\n",a['R'-'A']);
        System.out.printf("Total frogs: %d\n",a['F'-'A']);
        System.out.printf("Percentage of coneys: %.2f %%\n",a['C'-'A']*100.0/sum);
        System.out.printf("Percentage of rats: %.2f %%\n",a['R'-'A']*100.0/sum);
        System.out.printf("Percentage of frogs: %.2f %%",a['F'-'A']*100.0/sum);
    }
}
posted on 2023-05-05 20:06  许七安gyg  阅读(12)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });