https://www.acmicpc.net/problem/1244
1244번: 스위치 켜고 끄기
첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩
www.acmicpc.net
생각해 보기
- 시작 점을 1번으로 잡는게 편합니다. (남학생의 배수 때문)
- 여학생의 경우 시작점을 바꾸고 한칸씩 전진하며 비교합시다.
- 스위치의 상태는 한 줄에 20개씩만 출력합니다.
코드
더보기
#include<iostream>
#include<vector>
using namespace std;
int N;
vector<int> state;
int T;
int s,n;
void input(){
cin>>N;
state.resize(N+2);
for(int i=1;i<=N;++i){
cin>>state[i];
}
}
void solution(){
cin>>T;
while(T--){
cin>>s>>n;
if(s==1){
int idx=1;
while(n*idx<=N){
state[n*idx]=state[n*idx]?0:1;
++idx;
}
}
else{
int idx=1;
state[n]=state[n]?0:1;
while(n-idx>0&&n+idx<=N&&state[n-idx]==state[n+idx]){
state[n-idx]=state[n-idx]?0:1;
state[n+idx]=state[n+idx]?0:1;
++idx;
}
}
}
for(int i=1;i<=N;++i){
cout<<state[i]<<" ";
if(i%20==0)
cout<<'\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
solution();
return 0;
}

728x90
'알고리즘 > 백준 문제' 카테고리의 다른 글
| 21921 - 블로그 (0) | 2023.03.02 |
|---|---|
| 20920 - 영단어 암기는 괴로워 (0) | 2023.02.28 |
| 1205 - 등수 구하기 (0) | 2023.02.27 |
| 20125 - 쿠키의 신체 측정 (0) | 2023.02.27 |
| 25757 - 임스와 함께하는 미니게임 (0) | 2023.02.24 |
댓글