给定一个 hashMap 最终输出最大值的键

 1 /**
 2  *
 3  * 类  描  述:机试题: 给定一个 hashMap 最终输出最大值的键
 4  * 作        者: 赵         鹏
 5  * 时        间:2017年7月4日 下午6:51:06
 6  */
 7
 8 public class Test {
 9
10     public static void main(String[] args) {
11
12         Map<Integer, Integer> hashMap = new HashMap<Integer , Integer>();
13
14         //给定一个hashmap
15         hashMap.put(1, 45);
16         hashMap.put(2, 6666);
17         hashMap.put(3, 15);
18         hashMap.put(4, 100);
19         hashMap.put(5, 3210);
20
21         //输出最大值的键
22         System.out.println(getMaxKey(hashMap));
23
24     }
25
26     public static String getMaxKey(Map<Integer , Integer> hashMap) {
27
28         int key   = 0;
29         int value = 0;
30
31         int flagKey   = 0;
32         int flagValue = 0;
33
34         Set<Entry<Integer,Integer>> entrySet = hashMap.entrySet();
35
36         for (Entry<Integer, Integer> entry : entrySet) {
37
38             //key value 代表每轮遍历出来的值
39             key   = entry.getKey();
40             value = entry.getValue();
41
42             if(flagValue < value ) {
43
44                 //flagKey flagValue 当判断出最大值是将最大值赋予该变量
45                 flagKey   = key;
46                 flagValue = value;
47
48             }
49
50         }
51
52         return String.valueOf(flagKey);
53     }
54
55 }
时间: 2024-08-26 09:06:01

给定一个 hashMap 最终输出最大值的键的相关文章

给定一个数值,输出符合中国人习惯的读法--记一道笔试题

题目:给定一个数字,最大小于一万亿,输出符合中国人习惯的读法,例如: a.12输出:十二 b.102输出:一百零二 c.1002输出:一千零二 d.112输出:一百十二 e.10112输出:一万零一百十二 f.120000000:一亿二千万 g.11021002:一千一百零二万一千零二 h.11020102:一千一百零二万零一百零二 i.1000001:一百万零一 j.1000000001:十亿零一 嗯,一道笔试题,解的很没有节操,没有太好的思路,只能尽力抽取翻译过程中的共有代码,尽量写的不那么

codeforces22c System Administrator【给定一个割顶输出边 BCC】

Description Bob got a job as a system administrator in X corporation. His first task was to connectn servers with the help ofm two-way direct connection so that it becomes possible to transmit data from one server to any other server via these connec

给定一个n,输出从1到n的整数

循环输出 function print(n) { for (let i = 1; i <= n; i++) { console.log(i); } } print(10); 递归输出 function print(n) { if (n) { print(n - 1); console.log(n); } } print(10); 原文地址:https://www.cnblogs.com/wynnzen/p/10349311.html

给定一个日期,输出这个日期是该年的第几天

#include<stdio.h> int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int judge(int a); int main() { int a,b,c,i,sum=0; while(scanf("%d/%d/%d",&a,&b,&c)==3) { sum=0; for(i=0;i<b-1;i++) { sum+=days[i]; if(judge(a) &&i

c语言:给定一个大写字母,用小写字母输出

给定一个大写字母,用小写字母输出 程序: #include<stdio.h> int main() { char   c1,c2; printf("请输入一个大写字母:"); scanf("%c", &c1); c2=c1+32; printf("c2=%c\nc2=%d\n", c2,c2); return 0; } 结果: 请输入一个大写字母:A c2=a c2=97 请按任意键继续. . .

给定一个字符串类型表示的小数,输出其二进制表示

题目 给定一个字符串类型(string)表示的小数,打印出它的二进制表示. 如果这个数无法精确地表示为二进制形式,输出”ERROR”. 解答 整数部分通过不断地对2取余然后除以2来得到其二进制表示, 或是不断地和1按位与然后除以2得到其二进制表示. 小数部分则通过不断地乘以2然后与1比较来得到其二进制表示. 小数部分转化为二进制,通过乘以2然后与1比较,大于等于1则该位为1,并且该值减去1: 否则该位为0.不断地通过这种操作最终能使该小数部分的值变为0的,即可精确表示. 否则将无法用有限的位数来

给定一个字符串,找到第一个只出现一次的字符的下标,找不到输出-1。

1. 给定一个字符串,找到第一个只出现一次的字符的下标,找不到输出-1. sample: 输入:"abcdefcba" 输出:3 解法:先遍历字符串,用一个map记录每个字符出现的次数,再次遍历字符串,找到第一个只出现一次的字符,复杂度为O(n). #include <iostream> #include <string> #include <cstring> #include <map> using namespace std; int

一道有趣的算法题:仿照Excel的列编号,给定一个数字,输出该列编号字符串

       By Long Luo 最近遇到一个算法题: 仿照Excel的列编号,给出一个数字,输出该列编号字符串. 例如:A对应1,Z对应26,AA对应27,AZ对应52 ...... 这个题目是一个典型的26进制思路去处理,但是这个题目里面有很多陷阱,在1, 26, 52等特殊情况进行考虑,经过晚上接近1个小时的编写,完成的代码如下: C++代码如下: #include <iostream> #include <string.h> using namespace std; /

【C语言】用结构体数组实现:有三个候选人,每个选民只能选一个人,编写一个选票程序,最终输出候选人的票数

//用结构体数组实现:有三个候选人,每个选民只能选一个人,编写一个选票程序,最终输出候选人的票数(假设有十个选民) #include <stdio.h> #include <stdio.h> struct Person //声明结构体 { char name[20]; int count; }leader[3]={"li",0,"zhang",0,"sun",0}; //定义结构体数组并初值化 int main() { i