当前位置:网站首页>Baidu written test 2022.4.12 + programming topic: simple integer problem
Baidu written test 2022.4.12 + programming topic: simple integer problem
2022-04-23 15:08:00 【White horse is not a horse·】
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));
// Input
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);
// Store a set of data in a map Go to the assembly .
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); // Will automatically overwrite
}
// Put the set into the array ( Traverse )
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();
}
// Sort the array
int m=0;
int n=0;
int result=0;
// Sort by the second column from large to small ,
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);
}
}
// Sum up your thoughts
1)Map Set up
2)Map Traversal of the set
package demo;
import java.util.*;
public class main3 {
public static void main(String[] args) {
// Change two sets of numbers into map aggregate
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);
// Count the number of elements in an array , And into map aggregate
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);
// The way to traverse
for(Map.Entry<String,Integer> entry: map.entrySet()){
String string=entry.getKey();
Integer integer=entry.getValue();
System.out.println("string="+string+" "+"integer="+integer);
}
}
}
版权声明
本文为[White horse is not a horse·]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407525215.html
边栏推荐
- 封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
- About UDP receiving ICMP port unreachable
- Alexnet model
- MySQL sync could not find first log file name in binary log index file error
- 牛客网数据库SQL实战详细剖析(26-30)
- Basic operation of sequential stack
- Three uses of kprobe
- Unity_ Code mode add binding button click event
- How to design a good API interface?
- 每日一题-LeetCode396-旋转函数-递推
猜你喜欢

Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
![[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection](/img/8c/e72e628a44a36bfc7406a002d00215.png)
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
![Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]](/img/c6/5241de0d670da3dae136a3047c6160.jpg)
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]

Comment eolink facilite le télétravail

Mysql连接查询详解

Lotus DB design and Implementation - 1 Basic Concepts

Thread synchronization, life cycle

冰冰学习笔记:一步一步带你实现顺序表

eolink 如何助力遠程辦公

asp. Net method of sending mail using mailmessage
随机推荐
Basic operation of sequential stack
牛客网数据库SQL实战详细剖析(26-30)
async关键字
Analysis of common storage types and FTP active and passive modes
UML learning_ Day2
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
js——实现点击复制功能
Lotus DB design and Implementation - 1 Basic Concepts
Will golang share data with fragment append
Share 20 tips for ES6 that should not be missed
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
Introduction to dirty reading, unrepeatable reading and phantom reading
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
thinkphp5+数据大屏展示效果
Advanced version of array simulation queue - ring queue (real queuing)
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
Thinkphp5 + data large screen display effect
Flink datastream type system typeinformation
免费在upic中设置OneDrive或Google Drive作为图床