当前位置:网站首页>百度笔试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
边栏推荐
- Some good articles on pthread multithreading
- 线程间控制之CountDownLatch和CyclicBarrier使用介绍
- jsp学习1
- VMware installation 64 bit XP Chinese tutorial
- pthread_ Why does self() repeat
- postman批量生产body信息(实现批量修改数据)
- STD:: map and STD:: vector memory free
- PySide2
- 金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
- 帆软中使用if else 进行判断-使用标题条件进行判断
猜你喜欢

On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication

利用json-server在本地创建服务器请求

帆软之单元格部分字体变颜色

Visio画拓扑图随记

RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复

星界边境文本自动翻译机(高级版)使用说明

Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)

使用Executors类快速创建线程池

Recyclerview advanced use (I) - simple implementation of sideslip deletion

关于云容灾,你需要知道这些
随机推荐
数据库DbVisualizer Pro报文件错误,导致数据连接失败
mysql 5.1升级到5.67
关于密匙传递的安全性和数字签名
MySQL数据库讲解(九)
Operation instructions of star boundary automatic text translator (advanced version)
云迁移的六大场景
JDBC和servlet写CRUD的接口总结
PySide2
Logging module
Multiple inheritance virtual base exercises
Postman的安装使用及填坑心得
MySQL数据库讲解(十)
void*是怎样的存在?
How QT designer adds resource files
帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
squid代理
Date的after时间判断
rsync+inotify远程同步
Oracle-数据泵使用
mysql 5.1升级到5.611