문제 URL : https://www.acmicpc.net/problem/10825
10825번: 국영수
첫째 줄에 도현이네 반의 학생의 수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 한 줄에 하나씩 각 학생의 이름, 국어, 영어, 수학 점수가 공백으로 구분해 주어진다. 점수는 1보다 크거나 같고, 1
www.acmicpc.net
문제 접근법 : stl을 이용하여 문제 그대로 정렬을 하면되는 문제입니다.
정렬을 사용할줄알고 그것을 정렬기준을 잡을줄 알면 되는문제이니 굉장히 쉽습니다.
3가지언어 모두 올려드리겠습니다.
소스코드:
c++코드
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
49
50
51
52
53
54
55
56
57
58
59
60
|
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
class member{
public :
member(){}
string name;
int korean;
int eng;
int math;
};
bool cmp(const member a,const member b)
{
if(a.korean>b.korean)
return true;
else if(a.korean==b.korean)
{
if(a.eng<b.eng)
return true;
else if(a.eng==b.eng)
{
if(a.math>b.math)
return true;
else if(a.math==b.math)
{
if(a.name<b.name)
return true;
}
}
}
return false;
}
int main()
{
int n;
scanf("%d",&n);
vector<member> v;
for(int i=0;i<n;i++)
{
member m;
cin>>m.name>>m.korean>>m.eng>>m.math;
v.push_back(m);
}
sort(v.begin(),v.end(),cmp);
for(int i=0;i<v.size();i++)
{
cout<<v[i].name;
printf("\n");
}
}
|
cs |
자바 소스
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
49
50
51
52
53
54
55
56
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Integer n = Integer.parseInt(br.readLine());
ArrayList<Student> list=new ArrayList<Student>();
for (int i = 0; i < n; i++) {
String s = br.readLine();
String input[] = s.split(" ");
String name = input[0];
Integer korean = new Integer(input[1]);
Integer eng = new Integer(input[2]);
Integer math = new Integer(input[3]);
list.add(new Student(name, korean, eng, math));
}
Collections.sort(list);
for(int i=0;i<list.size();i++)
{
bw.write(list.get(i).name+"\n");
}
bw.close();
br.close();
}
}
class Student implements Comparable<Student> {
String name;
int korean, math, eng;
public Student(String name, int korean, int eng, int math) {
// TODO Auto-generated constructor stub
this.name = name;
this.korean = korean;
this.math = math;
this.eng = eng;
}
public int compareTo(Student a) {
if(this.korean==a.korean&&this.math==a.math&&this.eng==a.eng)
return this.name.compareTo(a.name);
if(this.korean==a.korean&&this.eng==a.eng)//수학점수가 내림차순
return a.math-this.math;
if(this.korean==a.korean)//영어점수가 오름차순
return this.eng-a.eng;
return a.korean-this.korean;//국어점수가 내림차순
}
}
|
cs |
파이썬 소스
n=int(input())
list = []
for i in range(0,n):
name,korean,eng,math = input().split();
list.append([name,int(korean),int(eng),int(math)])
list = sorted(list, key= lambda x : (-x[1],x[2],-x[3],x[0]))
for i in range(0,n):
print(list[i][0])
파이썬 코드는 가장 짧은데 아직 입숙하지 않아서 저한텐 좀 버겁더라구여
코드는 길지만 저한텐 c++이 더 편한가 봅니다 ㅋㅋㅋㅋ
질문은 언제든지 환영입니다.
모르는것 또는 궁금한점은 댓글을 이용해주시길 바랍니다.
'백준' 카테고리의 다른 글
백준 16948 데스 나이트 (0) | 2020.07.31 |
---|---|
백준 1463 1로 만들기 (0) | 2020.07.27 |
백준 17834 사자와 토끼 (0) | 2020.07.26 |
백준 1707 이분그래프 (0) | 2020.07.26 |
백준 17835 면접보는 승범이네 (0) | 2020.07.26 |