# Huawei online programming question series-8-consolidated table record

Problem Description:

## 1. The problem involves knowledge points

• sort
• Multiple data training

## 2. Solve by yourself

• Data is stored in a priority queue, sorted by the first element
• Traversing the mailbox queue adds the first of two consecutive elements
```package com.chaoxiong.niuke.huawei;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

/**
* Create by tianchaoxiong on 18-5-2.
*/
public class HuaWei_8_3 {
private int key1;
private int key2;

private int getKey1() {
return key1;
}
public int getKey2() {
return key2;

}

private HuaWei_8_3(int key1, int key2) {
this.key1 = key1;
this.key2 = key2;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Comparator<HuaWei_8_3>OrderIsdn = new Comparator<HuaWei_8_3>() {
@Override
public int compare(HuaWei_8_3 o1, HuaWei_8_3 o2) {
int a = o1.getKey1();
int b = o2.getKey1();
return Integer.compare(a, b);//From big to small
}
};
while (scanner.hasNextInt()){
int N = scanner.nextInt();
Queue<HuaWei_8_3>priorityQueue = new PriorityQueue<HuaWei_8_3>(2,OrderIsdn);
for(int i=0;i<N;i++){
HuaWei_8_3 huaWei_8_3 = new HuaWei_8_3(scanner.nextInt(),scanner.nextInt());
}
// Make a comparison print
int [][]intArr = new int[N][2];
int intArrIndex = 0;
while (priorityQueue.peek() != null) {
HuaWei_8_3 tmp = priorityQueue.poll();
int a1 = tmp.getKey1();
int a2 = tmp.getKey2();
if(intArrIndex==0){
intArr[intArrIndex][0] = a1;
intArr[intArrIndex][1] = a2;
intArrIndex++;
}else {
if(a1==intArr[intArrIndex-1][0]){
intArr[intArrIndex-1][0] = a1;
intArr[intArrIndex-1][1] = intArr[intArrIndex-1][1]+a2;
}else {
intArr[intArrIndex][0] = a1;
intArr[intArrIndex][1] = a2;
intArrIndex++;
}
}
}
//Printing
for(int i=0;i<intArrIndex;i++){
System.out.println(intArr[i][0]+" "+intArr[i][1]);
}
}
}
}
```

map

## 4. Summary of this question

In this paper, I use a priority queue to store the data. I use the characteristics of mailbox queue which is not unique and orderly
During traversal, the same key2 of key1 is accumulated, and other data structures can be used for optimization

Tags: Java

Posted on Fri, 20 Mar 2020 09:23:46 -0700 by nwoottonn