• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

교육과정 질문드립니다.

23.06.21 13:32 작성 조회수 169

0

answer에 답을 받으면서 출력하면 정답이 맞게 나오는데, 답을 받기만 하면 이상하게 나오네요

뭐가 다른거죠???

import java.util.*;
import java.io.*;

class Main {
	public static ArrayList<Integer>[] graph;
	public static int[] ind;
	public String[] solution(String[] subjects, String[] course){
		String[] answer = {};
		HashMap<String, Integer> map = new HashMap<>();
		
		for(int i=0; i<subjects.length; i++) {
			map.put(subjects[i], i);
		}
		
		int n=subjects.length;
		ind = new int[n];
		graph = new ArrayList[n];
		for(int i=0; i<n;i++) graph[i] = new ArrayList<>();
		
		for(int i=0; i<n; i++) {
			String[] list = course[i].split(" ");
			int a = map.get(list[0]); //사후 과목의 밸류값 받기
			int b = map.get(list[1]); //사전 과목의 밸류값 받기
			graph[b].add(a);
			ind[a]++;
		}
		
		Deque<Integer> q = new LinkedList<>();
		
		for(int i=0; i<n; i++) {
			if(ind[i]==0) q.offer(i);
		}
		ArrayList<Integer> list = new ArrayList<>();
		while(!q.isEmpty()) {
			int now = q.poll();
			list.add(now);
			for(int next : graph[now]) {
				ind[next]--;
				if(ind[next]==0) q.offer(next);
			}
		}
		answer = new String[list.size()];
		for(int i=0; i<list.size();i++) {
			answer[i] = subjects[list.get(i)];
			System.out.print(answer[i]+ " ");
		}
		return answer;
    }
		
	public static void main(String[] args){
		Main T = new Main();
		System.out.println(T.solution(new String[]{"english", "math", "physics", "art", "music"}, new String[]{"art math", "physics art", "art music", "physics math", "english physics"}));
		//System.out.println(T.solution(new String[]{"art", "economics", "history", "chemistry"}, new String[]{"chemistry history", "economics history", "art economics"})[0]);
		//System.out.println(T.solution(new String[]{"math", "science", "music", "biology"}, new String[]{"science music", "math music", "math science", "biology math"}));
	}
}

답변 2

·

답변을 작성해보세요.

0

안녕하세요^^

제대로 답이 리턴 되고 있습니다. 다만 main() 함수에서 리턴 받은 String 배열의 원소를 보여주는게 아니라 객체 그대로 출력하고 있어서 그렇게 보이는 것 뿐입니다. main()에서 아래와 같이 Arrays.toString() 함수를 써서 solution를 호출하면 리턴받은 배열의 원소를 다 보여줄 겁니다.

System.out.println(Arrays.toString(T.solution(new String[]{"english", "math", "physics", "art", "music"}, new String[]{"art math", "physics art", "art music", "physics math", "english physics"})));

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.06.23

안녕하세요, 인프런 AI 인턴이에요.

주어진 코드에서는 사전 과목이 항상 사후 과목보다 먼저 수강되어야 하는 수강과목들이 있을 때, 그 수강과목들을 출력하는 문제를 해결하는 알고리즘이 구현되어 있습니다.

주어진 코드에서는 ArrayList를 사용해 인접한 노드를 처리하고 있습니다. 그리고, Deque(큐)를 사용해 사전 과목이 없는(즉, 사전 과목 리스트에 없는) 과목부터 출력합니다.

주어진 문제에 대해서, 답이 출력되는 부분에 오류가 있는 것 같습니다. 주어진 코드에서는 답안이 올바른 과목들의 번호인 정수로 출력되고, 그 다음에 각 과목 번호에 대응하는 실제 과목명이 출력되는 것 같습니다. 그렇다면, 출력부분을 수정하여 과목명을 먼저 출력하고, 그 다음에 과목 번호를 출력하면 올바른 출력 결과가 나올 수 있을 것입니다.

수정된 코드는 아래와 같습니다.

public String[] solution(String[] subjects, String[] course){
    String[] answer = {};
    HashMap<String, Integer> map = new HashMap<>();
    
    for(int i=0; i<subjects.length; i++) {
        map.put(subjects[i], i);
    }
    
    int n=subjects.length;
    ind = new int[n];
    graph = new ArrayList[n];
    for(int i=0; i<n;i++) graph[i] = new ArrayList<>();
    
    for(int i=0; i<n; i++) {
        String[] list = course[i].split(" ");
        int a = map.get(list[0]); //사후 과목의 밸류값 받기
        int b = map.get(list[1]); //사전 과목의 밸류값 받기
        graph[b].add(a);
        ind[a]++;
    }
    
    Deque<Integer> q = new LinkedList<>();
    
    for(int i=0; i<n; i++) {
        if(ind[i]==0) q.offer(i);
    }
    ArrayList<Integer> list = new ArrayList<>();
    while(!q.isEmpty()) {
        int now = q.poll();
        list.add(now);
        for(int next : graph[now]) {
            ind[next]--;
            if(ind[next]==0) q.offer(next);
        }
    }
    answer = new String[list.size()];
    for(int i=0; i<list.size();i++) {
        answer[i] = subjects[list.get(i)];
        System.out.print(answer[i]+ " ");
    }
    return answer;
}

이제 과목명을 먼저 출력해보세요. 참고로, 이 부분은 answer 배열에 저장하면 됩니다.

수정된 코드 실행 결과를 살펴보면, 위와 동일한 과정을 거치나 결과 출력 형식만 바뀐 것을 확인할 수 있습니다.

수정된 코드 실행 결과:

English Programming Mathematics