CCF/CSP 201312-1出现次数最多的数(满分题解Java版)

CCF 考试,一定要刷历年真题。
在提交代码的时候,一定不要把中文注释提交上去了,可能会编译报错

  1. 题目描述
    201312-1出现次数最多的数
    CCF/CSP 201312-1出现次数最多的数(满分题解Java版)_第1张图片
  2. Java题解
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * 
 * @author wangdong
 *
 */
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		scanner.nextLine();
		String str = scanner.nextLine();
		scanner.close();
		HashMap map = new HashMap(num);
		String[] numsStrings = str.split(" ");
		for (int i = 0; i < num; i++) {
			Integer keyInteger = Integer.valueOf(numsStrings[i]);
			if (map.containsKey(keyInteger)) {
				Integer tar = map.get(keyInteger);
				map.put((keyInteger), map.get(keyInteger)+1);
			}else {
				map.put(keyInteger, 1);
			}
		}
		List keyStrings = new ArrayList();
		
		Integer maxNume = 0;
		for (Integer integer : map.keySet()) {
			Integer nuInteger = map.get(integer);
			if (nuInteger > maxNume) {
				maxNume = nuInteger;
			}
		}
		
		
		for (Integer integer : map.keySet()) {
			Integer nuInteger = map.get(integer);
			if (nuInteger == maxNume) {
				keyStrings.add(integer);
			}
		}
		
		int result = keyStrings.get(0);
		if (keyStrings.size()>1) {
			for (int i = 1; i < keyStrings.size(); i++) {
				if (keyStrings.get(i)

好啦,祝大家都能学习愉快!

你可能感兴趣的:(算法,CCF/CSP)