原文链接:华为OD机试Python - 剩余银饰的重量-CSDN博客
解题思路
这题用优先队列
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int num = sc.nextInt();
Queue<Integer> que = new PriorityQueue<Integer>((a,b) ->b-a);
for(int i=0; i< num; i++) {
que.offer(sc.nextInt());
}
int result=-1;
while(!que.isEmpty()) {
if(que.size() >= 3) {
int z = que.poll();
int y = que.poll();
int x = que.poll();
if(z -y !=0) {
que.offer(z-y);
}
if(y-x != 0) {
que.offer(y-x);
}
}else {
if( que.size() == 1) {
result = que.poll();
}else {
result = Math.max(que.poll(), que.poll());
}
}
}
System.out.println(result);
}
}