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

線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口

Subscription number development of wechat applet (message push)

关于密匙传递的安全性和数字签名

ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory

Research on recyclerview details - Discussion and repair of recyclerview click dislocation

使用Executors类快速创建线程池

squid代理

HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能

Visio installation error 1:1935 2: {XXXXXXXX

困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
随机推荐
线程间控制之Semaphore使用介绍
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
JDBC details
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
squid代理
帆软实现分页时第一行和最后两行冻结方式
Idea控制台乱码解决
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
FBS (fman build system) packaging
不同时间类型的执行计划计算
Multiple inheritance virtual base exercises
mysql锁数据库锁
Request module
OpenStack命令操作
mysql 5.1升级到5.69
Can I compile the header file and source file of the template separately
在Clion中给主函数传入外部参数
Detailed tutorial on the use of setinterval timing function of wechat applet
正则表达式