본문 바로가기

Java/SWEA

[JAVA] SWEA 13038. 교환학생

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AXxNn6GaPW4DFASZ&categoryId=AXxNn6GaPW4DFASZ&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=JAVA&select-1=3&pageSize=10&pageIndex=4

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

 

 

 

 

정답 

 

import java.util.*;

class Solution
{
	public static void main(String args[]) throws Exception
	{
		
		Scanner sc = new Scanner(System.in);
		int T =sc.nextInt();
		
		for(int tc = 1; tc <= T; tc++)
		{
			int N = sc.nextInt(); //들어야 하는 수업 수 
			
			int arr[] = new int[7];
			//수업 입력받기
			for(int i=0; i<7; i++)
			{
				arr[i] = sc.nextInt();
			}
			
			int answer = Integer.MAX_VALUE;
			for(int i=0; i<7; i++)
			{
				if(arr[i]==0)
					continue;
				int count=0; 
				int start = i; //시작 요일
				
				while(true)
				{
					//현재 요일에 수업이 있는 경우
					if(arr[start%7]==1)
						count++;
					start++; //다음 요일로 이동
					
					//필요한 수업 수를 모두 들은 경우 
					if(count==N)
					{
						answer = Math.min(answer, start-i);
						break;
					}
				}
			}
			
			
			System.out.printf("#%d %d\n",tc,answer);
		}
	}
}