본문 바로가기

Algorithms/SW Expert Academy

[Java] SW Expert Academy 1220번 문제: [S/W 문제해결 기본] 5일차 - Magnetic (Greedy)

--- 문제 ---

 

  • 1220. [S/W 문제해결 기본] 5일차 - Magnetic

 

--- 코드 ---

 

한 열(세로)에서 전에 1나오고 다음 2가 나오는 구간이 몇개인지를 세면 되는 문제였습니다.

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Sw1220 {

	public static void main(String[] args) throws FileNotFoundException {


		System.setIn(new FileInputStream("./src/1220.txt"));

		Scanner sc = new Scanner(System.in);
		int T;
		T=10;
				
		for(int test_case = 1; test_case <= T; test_case++) {
			
			int N = sc.nextInt();
			int[][] map = new int[N][N];
			
			for(int i=0; i<N; ++i) {
				for(int j=0; j<N; ++j) {
					map[i][j] = sc.nextInt();
				}
			}
			int sum = 0;
			for(int i=0; i<N; ++i) {
				int pre = map[0][i];
				int bottle = 0;
				for(int j=1; j<N; ++j) {
					int cur = map[j][i];
					if(cur==0) {
						continue;
					}
					if(pre==1 && cur == 2) {
						++bottle;
					}
					pre = cur;
				}
				sum += bottle;
				
			}
			
			System.out.printf("#%d %d%n",test_case,sum);
		}
		
	}

}

 

 

--- 출처 ---

 

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14hwZqABsCFAYD&categoryId=AV14hwZqABsCFAYD&categoryType=CODE&problemTitle=1220&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1

 

SW Expert Academy

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

swexpertacademy.com

 

반응형