강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

이혜정님의 프로필 이미지
이혜정

작성한 질문수

자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비

12. 멘토링

시간초과 에러

작성

·

256

0

import java.util.Scanner;
  
public class Main {
  public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int headCnt = sc.nextInt();
		int testCnt = sc.nextInt();
		int[][] array = new int[testCnt][headCnt];
		for(int i=0; i<testCnt; i++) {
			for(int j=0; j<headCnt; j++) {
				array[i][j] = sc.nextInt();
			}
		}
		
		System.out.print(solution(headCnt, testCnt, array));
	}
	
	public static int solution(int headCnt, int testCnt, int[][] arr) {
		int answer = 0;
		
		for(int i=1; i<=headCnt; i++) {
			for(int j=1; i<=headCnt; j++) {
				int cnt=0;
				for(int k=0; k<testCnt; k++) {
					int pi = 0, pj = 0;
					for(int s=0; s<headCnt; s++) { //등수
						if(arr[k][s] == i) pi = s;
						if(arr[k][s] == j) pj = s;
					}
					if(pi < pj) cnt++;
				}
				if(cnt == testCnt) {
					answer++;
				}
			}
		}
		return answer;
	}
}

왜 에러가 나는지 잘 모르겠습니다

답변 1

1

김태원님의 프로필 이미지
김태원
지식공유자

안녕하세요^^

3중 for문에서 오타입니다. 문제에 있는 입력을 넣어보세요. 멈추지 않을 겁니다.

이혜정님의 프로필 이미지
이혜정

작성한 질문수

질문하기