문제 링크입니다: https://www.acmicpc.net/problem/1244
1244번: 스위치 켜고 끄기
첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩 있다. 셋째 줄에는 학생수가 주어진다. 학생수는 100 이하인 양의 정수이다. 넷째 줄부터 마지막 줄까지 한 줄에 한 학생의 성별, 학생이 받은 수가 주어진다. 남학생은 1로, 여학생은 2로 표시하고, 학생이 받은 수는 스위치 개수 이하인 양의 정수이다. 학생의 성
www.acmicpc.net
문제에서 주어진 조건대로 코드를 작성하면 되는 문제였습니다.
보통 여학생이 주어졌을 때 헤매셨을 것이라고 생각하는데 for문을 적절히 사용하시면 간단하게 문제를 해결할 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
const int MAX_INDEX = 20; | |
const int MAX = 100 + 1; | |
int N; | |
bool switches[MAX]; | |
void pressSwitch(int idx) | |
{ | |
switches[idx] = 1 - switches[idx]; | |
} | |
bool isEndOfLine(int idx) | |
{ | |
return (idx % MAX_INDEX == 0 || idx == N); | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
cin >> N; | |
for (int i = 1; i <= N; i++) | |
{ | |
cin >> switches[i]; | |
} | |
int inputs; | |
cin >> inputs; | |
for (int i = 0; i < inputs; i++) | |
{ | |
int gender, num; | |
cin >> gender >> num; | |
if (gender == 1) | |
{ | |
for (int j = num; j <= N; j += num) | |
{ | |
pressSwitch(j); | |
} | |
continue; | |
} | |
pressSwitch(num); | |
for (int j = num - 1, k = num + 1; (j >= 1 && k <= N && switches[j] == switches[k]); j--, k++) | |
{ | |
pressSwitch(j); | |
pressSwitch(k); | |
} | |
} | |
for (int i = 1; i <= N; i++) | |
{ | |
cout << switches[i]; | |
if (isEndOfLine(i)) | |
{ | |
cout << "\n"; | |
continue; | |
} | |
cout << " "; | |
} | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 15970번 화살표 그리기 (0) | 2020.02.14 |
---|---|
백준 5639번 이진 검색 트리 (2) | 2020.02.07 |
백준 13459번 구슬 탈출 (0) | 2020.02.05 |
백준 2615번 오목 (0) | 2020.02.03 |
백준 17299번 오등큰수 (0) | 2020.01.31 |