当前位置:网站首页>百度笔试2022.4.12+编程题目:简单整数问题
百度笔试2022.4.12+编程题目:简单整数问题
2022-04-23 14:08:00 【白马非马·】
package demo;
import java.util.*;
public class Main1 {
public static void main(String[] args) {
//List<Integer> list=new ArrayList<>(Arrays.asList(1,2,1,3,5,4,2,1,3,3));
//输入
Scanner cin=new Scanner(System.in);
int size=cin.nextInt();
cin.nextLine();
String string=cin.nextLine();
String[] arrstring= string.split(" ");
List<Integer> list=new ArrayList<>();
for(int i=0;i<arrstring.length;i++){
list.add(Integer.parseInt(arrstring[i])) ;
}
System.out.println(list);
//把一个集合的数据存放到一个map集合中去。
Map<Integer,Integer> map = new TreeMap();
for(int i=0;i<list.size();i++){
Integer count=1;
if(map.containsKey(list.get(i))){
count=map.get(list.get(i));
count++;
}
map.put(list.get(i),count); //会进行自动覆盖
}
//把集合装进数组中(遍历)
int[][] data=new int[map.size()][2];
int i=0;
for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
data[i][0] =entry.getKey();
data[i++][1] =entry.getValue();
}
//对数组进行排序
int m=0;
int n=0;
int result=0;
//按第二列从大到小排序,
Arrays.sort(data,(a,b)->{
if(a[1]==b[1]) return b[0]-a[0];
return b[1]-a[1];
});
System.out.println(Arrays.deepToString(data));
m=data[0][0];
Arrays.sort(data,(a,b)->{
if(a[1]==b[1]) return a[0]-b[0];
return a[1]-b[1];
});
System.out.println(Arrays.deepToString(data));
n=data[0][0];
result=m-n;
System.out.println("result="+result);
}
}
//自我总结思想
1)Map集合的建立
2)Map集合的遍历
package demo;
import java.util.*;
public class main3 {
public static void main(String[] args) {
//把两组数变为map集合
List<String> name=new ArrayList<>(Arrays.asList("a","b","c","d"));
List<Integer> age=new ArrayList<>(Arrays.asList(1,2,3,4));
Map<String,Integer> map=new TreeMap<>();
for(int i=0;i<name.size();i++){
map.put(name.get(i),age.get(i));
}
System.out.println(map);
//统计一个数组中元素的个数,并变为map集合
List<Integer> data=new ArrayList<>(Arrays.asList(1,2,3,3,2,1,2,3,4,5,6,6,5,4,3));
Map<Integer,Integer> map1=new TreeMap<>();
for(int i=0;i<data.size();i++){
Integer count=1;
if(map1.containsKey(data.get(i))){
count=map1.get(data.get(i));
count++;
}
map1.put(data.get(i),count);
}
System.out.println(map1);
//遍历的方式
for(Map.Entry<String,Integer> entry: map.entrySet()){
String string=entry.getKey();
Integer integer=entry.getValue();
System.out.println("string="+string+" "+"integer="+integer);
}
}
}
版权声明
本文为[白马非马·]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42974034/article/details/124135681
边栏推荐
猜你喜欢
帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
Logback logger and root
MySQL-InnoDB-事务
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
帆软调用动态传参的方法,在标题中设置参数
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
Installation and use of postman pit
Wechat applet positioning and ranging through low-power Bluetooth device (2)
Promtail + Loki + Grafana 日志监控系统搭建
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
随机推荐
openstack理论知识
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
JDBC详解
postman批量生产body信息(实现批量修改数据)
Understand the concepts of virtual base class, virtual function and pure virtual function (turn)
星界边境文本自动翻译机使用说明
MySQL数据库讲解(十)
squid代理
使用DialogFragment的一些感受及防踩坑经验(getActivity、getDialog为空,cancelable无效等)
MySQL数据库讲解(九)
rsync+inotify远程同步
报表FCRA考试题集及答案(错了11题)
redis数据库讲解(四)主从复制、哨兵、Cluster群集
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
更改plsql工具栏的图标大小
按实际取,每三级分类汇总一次,看图知需求
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
MySQL数据库讲解(八)
Can I compile the header file and source file of the template separately
MySQL-InnoDB-事务