zoukankan      html  css  js  c++  java
  • 华为机试-票数统计

    题目描述
    请实现接口:
    unsigned int AddCandidate (char* pCandidateName);
    功能:设置候选人姓名
    输入: char* pCandidateName 候选人姓名
    输出:无
    返回:输入值非法返回0,已经添加过返回0 ,添加成功返回1

    Void Vote(char* pCandidateName);
    功能:投票
    输入: char* pCandidateName 候选人姓名
    输出:无
    返回:无

    unsigned int GetVoteResult (char* pCandidateName);
    功能:获取候选人的票数。如果传入为空指针,返回无效的票数,同时说明本次投票活动结束,释放资源
    输入: char* pCandidateName 候选人姓名。当输入一个空指针时,返回无效的票数
    输出:无
    返回:该候选人获取的票数

    void Clear()
    // 功能:清除投票结果,释放所有资源
    // 输入:
    // 输出:无
    // 返回

    输入描述:
    输入候选人的人数,第二行输入候选人的名字,第三行输入投票人的人数,第四行输入投票。
    输出描述:
    每行输出候选人的名字和得票数量。
    示例1
    输入

    4
    A B C D
    8
    A B C D E F G H
    输出

    A : 1
    B : 1
    C : 1
    D : 1
    Invalid : 4

    程序实现

    1. import java.util.ArrayList;  
    2. import java.util.List;  
    3. import java.util.Scanner;  
    4.   
    5. /** 
    6.  * 题目描述 请实现接口: unsigned int AddCandidate (char* pCandidateName); 功能:设置候选人姓名 输入: 
    7.  * char* pCandidateName 候选人姓名 输出:无 返回:输入值非法返回0,已经添加过返回0 ,添加成功返回1 
    8.  *  
    9.  * Void Vote(char* pCandidateName); 功能:投票 输入: char* pCandidateName 候选人姓名 输出:无 
    10.  * 返回:无 
    11.  *  
    12.  * unsigned int GetVoteResult (char* pCandidateName); 
    13.  * 功能:获取候选人的票数。如果传入为空指针,返回无效的票数,同时说明本次投票活动结束,释放资源 输入: char* pCandidateName 
    14.  * 候选人姓名。当输入一个空指针时,返回无效的票数 输出:无 返回:该候选人获取的票数 
    15.  *  
    16.  * void Clear() // 功能:清除投票结果,释放所有资源 // 输入: // 输出:无 // 返回 
    17.  *  
    18.  * 输入描述: 输入候选人的人数,第二行输入候选人的名字,第三行输入投票人的人数,第四行输入投票。 输出描述: 每行输出候选人的名字和得票数量。 示例1 输入 
    19.  *  
    20.  * 4 A B C D 8 A B C D E F G H 输出 
    21.  *  
    22.  * A : 1 B : 1 C : 1 D : 1 
    23.  *  
    24.  */  
    25. public class Main {  
    26.     static List<String> candiList = new ArrayList<>();  
    27.     static List<Integer> votess = new ArrayList<>();  
    28.   
    29.     public static void main(String[] args) {  
    30.         Scanner scanner = new Scanner(System.in);  
    31.         while (scanner.hasNext()) {  
    32.             int addResult = 0;  
    33.             String candidateNum1 = scanner.nextLine();  
    34.             String candidate = scanner.nextLine();  
    35.             String voteNum1 = scanner.nextLine();  
    36.             String vote = scanner.nextLine();  
    37.             String[] candidates = candidate.split(" ");  
    38.             String[] votes = vote.split(" ");  
    39.             int candidateNum = Integer.parseInt(candidateNum1);  
    40.             int voteNum = Integer.parseInt(voteNum1);  
    41.   
    42.             for (int i = 0; i < candidates.length; i++) {  
    43.                 addResult = AddCandidate(candidates[i]);  
    44.             }  
    45.             candiList.add("Invalid");  
    46.             votess.add(0);  
    47.   
    48.             for (int i = 0; i < votes.length; i++) {  
    49.   
    50.                 Vote(votes[i]);  
    51.             }  
    52.             int numbs = 0;  
    53.             for (int i = 0; i < candidateNum; i++) {  
    54.                 String c = candiList.get(i);  
    55.                 numbs = GetVoteResult(c);  
    56.                 System.out.println(c + " : " + numbs);  
    57.             }  
    58.             System.out.println("Invalid" + " : " + votess.get(candiList.indexOf("Invalid")));  
    59.             Clear();  
    60.         }  
    61.   
    62.     }  
    63.   
    64.     // 设置候选人姓名  
    65.     public static int AddCandidate(String pCandidateName) {  
    66.         if (pCandidateName == " ") {  
    67.             return 0;  
    68.         }  
    69.   
    70.         if (!candiList.contains(pCandidateName)) {  
    71.             candiList.add(pCandidateName);  
    72.             votess.add(0);  
    73.             return 1;  
    74.         } else {  
    75.             return 0;  
    76.         }  
    77.   
    78.     }  
    79.   
    80.     // 功能:投票  
    81.     public static void Vote(String pCandidateName) {  
    82.         int index = 0;  
    83.         if (candiList.contains(pCandidateName)) {  
    84.             index = candiList.indexOf(pCandidateName);  
    85.             votess.set(index, votess.get(index) + 1);  
    86.         } else {  
    87.             index = candiList.indexOf("Invalid");  
    88.             votess.set(index, votess.get(index) + 1);  
    89.         }  
    90.     }  
    91.   
    92.     // 功能:获取候选人的票数。  
    93.     public static int GetVoteResult(String pCandidateName) {  
    94.         int index = candiList.indexOf(pCandidateName);  
    95.   
    96.         return votess.get(index);  
    97.   
    98.     }  
    99.   
    100.     // 功能:清除投票结果,释放所有资源  
    101.     public static void Clear() {  
    102.         candiList.clear();  
    103.         votess.clear();  
    104.     }  
    105.   
    106. }  
  • 相关阅读:
    Zuul token FIlter 验证失败结果输出
    springboot 使用 dev tool 导致 CastException
    索引失效的情况汇总
    JVM笔记-GC常用参数设置
    关于gdb和shp的FID问题
    配置mac百度云同步盘
    【python常用函数1】
    【python环境配置1】 环境变量与常用模块
    【nodejs笔记——小知识点汇总】
    ArcGIS标注
  • 原文地址:https://www.cnblogs.com/wwjldm/p/7158673.html
Copyright © 2011-2022 走看看