1244번: 스위치 켜고 끄기
첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩
www.acmicpc.net
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt()+1;
int[] sw = new int[n];
for (int i = 1; i < n; i++) {
sw[i] = sc.nextInt();
}
int student = sc.nextInt();
for(int j = 1; j<= student; j++) {
int gender = sc.nextInt();
int key = sc.nextInt();
//남자라면
if(gender == 1) {
//key의 배수로
for(int i = key; i<n; i+=key) sw[i]^=1;//xor(다르면 1)
}
//여자라면
else if(gender == 2){
int l = key-1;
int r = key+1;
while(true) {//대칭 찾아서
if(l<1 || r>= n) break;
if(sw[l] != sw[r]) break;
l--; r++;
}
l++; r--;
while(l<=r) {
sw[l] ^=1;//xor
l++;
}
}
}
for (int i = 1; i < n; i++) {
System.out.print(sw[i]+" ");
if(i%20==0) System.out.println();
}
}
}
'ALGORITHM > 프로그래머스 | 백준 | 삼성 | 카카오' 카테고리의 다른 글
[프로그래머스] 스타 수열 java (0) | 2021.04.05 |
---|---|
[프로그래머스] 짝지어 제거하기 java (0) | 2021.04.01 |
[백준] 1543 문서 검색 java (0) | 2021.03.29 |
[백준] 평범한 배낭 java (0) | 2021.03.25 |
[프로그래머스] 등굣길 (0) | 2021.03.25 |