회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
compare 값 비교
1713번 - 후보 추천하기
arty0928
2년 전
0
Candidate에서 compare 부분의 주석 처리코드로 하면 틀렸습니다! 가 나오고, 주석처리 안한 아래의 코드로 compare를 하면 맞았습니다! 가 나옵니다. 주석 처리 한 부분과 안 한 코드의 compare 연산 결과는 동일하지 않나요? 결과는 동일한 것 같은데 왜 하나는 틀리고 하나는 맞을까요? 제가 잘못 알고 있는 부분이 있다면 알려주시면 감사하겠습니다!!
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static class Candidate implements Comparable<Candidate>{ int num ; int timestamp; int count; boolean isIn; @Override public String toString() { return "[num=" + num + ", timestamp=" + timestamp + ", count=" + count + ", isIn=" + isIn + "]"; } public Candidate(int num, int timestamp, int count, boolean isIn) { super(); this.num = num; this.timestamp = timestamp; this.count = count; this.isIn = isIn; } @Override public int compareTo(Candidate o) { // TODO Auto-generated method stub // int result = Integer.compare(this.count, count); // // if(result == 0) { // return Integer.compare(this.timestamp, o.timestamp); // } // return result; // if(this.count == o.count) { return this.timestamp - o.timestamp; } return this.count - o.count; } } static int N; static int M; static Candidate[] candidates; static List<Candidate> list; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(br.readLine()); M = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); list = new ArrayList<Candidate>(); candidates = new Candidate[101]; for (int i = 1; i <= M; i++) { int num = Integer.parseInt(st.nextToken()); //1. 처음 추천 입력 if(candidates[num] == null) { candidates[num] = new Candidate(num,0,0,false); } //2. 이미 게시된 경우 if(candidates[num].isIn) { candidates[num].count++; } //없는 숫자가 들어올 경우 else { candidates[num].timestamp = i; candidates[num].count = 1; candidates[num].isIn = true; if(list.size() ==N ) { Collections.sort(list); list.get(0).isIn = false; list.remove(0); } list.add(candidates[num]); } } Collections.sort(list,new Comparator<Candidate>() { @Override public int compare(Candidate o1, Candidate o2) { // TODO Auto-generated method stub return o1.num - o2.num; } }); for (int j = 0; j < list.size(); j++) { System.out.print(list.get(j).num +" "); } } }
protarie
2년 전
0
39번줄에 o.count가 아니라 그냥 count라고 쓴 게 잘못된 부분인 거 같은데
자바는 잘 모르겠네요
댓글을 작성하려면
로그인
해야 합니다.
arty0928 2년 전