Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

끄적끄적

백준 1244 스위치 켜고끄기 본문

알고리즘

백준 1244 스위치 켜고끄기

yenacathy97 2021. 8. 6. 21:10

인덱스를 접근하는 것에서 또한 문제에서 20마다 줄바꿈 해주는것도 유의해서 봤어야했다.

  • 배운점
    • 백준에서 마지막에 공백이 들어가도 인정해 주는것을 알게되었다.
    • 배수를 검색할때 for문에서 i++이 아닌 i+=index 로 뛰면 배수만 검색할 수 있다.
    • 0부터 시작하는 배열로 인에 혼동이 올때는 1부터 입력받자
package day0802;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Q1244 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int n= Integer.parseInt(bf.readLine());
		int []arr = new int[n+1];
		String s = bf.readLine();
		StringTokenizer st= new StringTokenizer(s);
		int i=1;
		while(st.hasMoreTokens()) {
			arr[i]=Integer.parseInt(st.nextToken());
			i++;
		}
		
		int student = Integer.parseInt(bf.readLine());
		for(int k= 0;k<student;k++) {
			s= bf.readLine();
			st=new StringTokenizer(s);
			int who = Integer.parseInt(st.nextToken());
			int index= Integer.parseInt(st.nextToken());
			
			if(who==1) {//남자
				for(int m=index;m<n+1;m+=index) {
					
						if(arr[m]==0) arr[m]=1;
						else arr[m]=0;
					
				}	
			}
			else {
				int bef=index;
				int aft=index;
				
				while(true) {
					if(bef<1 || aft>=n+1 ) break;
					if(arr[bef]!=arr[aft] ) break;
						
					bef--;
					aft++;
				}
				bef++;
				aft--;

				for(int m=bef;m<=aft;m++) {
					if(arr[m]==0) arr[m]=1;
					else arr[m]=0;
				}
			}

		}
		for(int t=1;t<n+1;t++) {
			
			 System.out.print(arr[t]+" ");
			 if(t%20==0) System.out.println();
		}
		
		}

}

'알고리즘' 카테고리의 다른 글

백준 2493 탑  (0) 2021.08.06
백준 17478 재귀함수가 뭔가요?  (0) 2021.08.06
SWE 원재의 메모리 복구하기  (0) 2021.08.06
SWE 달뱅이 숫자  (0) 2021.08.06
SWE Ladder1  (0) 2021.08.06
Comments