Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
48 lines (44 loc) · 1.08 KB

File metadata and controls

48 lines (44 loc) · 1.08 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package tencent;
import java.util.Arrays;
import java.util.Scanner;
/**
* 腾讯2018秋招模拟笔试
*
* 有一组数字,两个人A和B依次从其中取出一个数字,每次都取出剩余数字中最大的一个,
* A先B后,计算A取出的数字之和-B取出的数字之和
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class Main1 {
/**
* 先排序,再依次求得A、B各自的分数
*
* @param a
* @return
*/
public static int done(int[] a){
int n=a.length;
if(n==0) return 0;
Arrays.sort(a);
int v1=0,v2=0;
int index=0;
for(int i=n-1;i>=0;i--){
if(index%2==0) v1+=a[i];
else v2+=a[i];
index++;
}
return v1-v2;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=in.nextInt();
}
int r=done(a);
System.out.println(r);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.