728x90
https://codeforces.com/problemset/problem/1092/B
# JAVA
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String str = br.readLine();
int num = Integer.parseInt(str);
int arr[] = new int[num+1];
str = br.readLine();
StringTokenizer st = new StringTokenizer(str);
int temp=1;
while(st.hasMoreTokens()) {
arr[temp]=Integer.parseInt(st.nextToken());
temp++;
}
Arrays.sort(arr);
int count =0;
for(int i=1;i<arr.length;i+=2) {
count +=arr[i+1]-arr[i];
}
bw.write(String.valueOf(count));
bw.flush();
bw.close();
br.close();
}
}
#C++
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int student_count, input_student_skill;
cin >> student_count;
vector<int> v;
for (int i = 0; i < student_count; i++) {
cin >> input_student_skill;
v.push_back(input_student_skill);
}
sort(v.begin(), v.end());
int sum = 0;
for (int i = 0; i < student_count; i += 2) {
sum += (v[i + 1] - v[i]);
}
cout << sum << endl;
return 0;
}
두 방법 다 비슷한 흐름으로 진행된다.
학생들의 skill을 배열 혹은 vector에 저장하여 이를 정렬한다.
728x90
'🐣 알고리즘 삐약 > ✏️ 냅다 덤벼보는 문제풀이' 카테고리의 다른 글
[C++] UVa 750 : 8 Queens Chess Problem (0) | 2022.04.20 |
---|---|
[CSES] Road_Construction | C++ (0) | 2022.04.20 |
[CSES] Road Reparation (0) | 2022.04.19 |
[C++] UVa-11572 Unique Snowflake (0) | 2022.04.16 |
UVa / 00514 Rails [JAVA] (0) | 2022.03.11 |