当前位置:网站首页>百度笔试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
边栏推荐
猜你喜欢

关于NodeJS中JSON5的相关配置和使用

Jmeter安装教程以及我遇到的问题的解决办法

帆软中单元格中隔行变色以及数量大于100字体变大变红设置

Postman的安装使用及填坑心得

金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务

Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)

VMware installation 64 bit XP Chinese tutorial

使用开源调研工具Prophet是一种什么体验?

01-nio basic ByteBuffer and filechannel

Idea控制台乱码解决
随机推荐
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
HyperMotion云迁移完成阿里云专有云产品生态集成认证
关于Jmeter启动闪退问题
OpenStack命令操作
MySQL数据库讲解(九)
Visio画拓扑图随记
DP - [noip2000] grid access
squid代理
帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
星界边境文本自动翻译机(高级版)使用说明
RecyclerView高级使用(二)-垂直拖拽排序的简单实现
帆软中使用if else 进行判断-使用标题条件进行判断
On September 8, the night before going to Songshan Lake
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
Jira截取全图
使用itextpdf实现截取pdf文档第几页到第几页,进行分片
DP energy Necklace
在MAC上安装mysql
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
倒计时1天~2022云容灾产品线上发布会即将开始