[알고리즘] 1244. 스위치 켜고 끄기
2024. 1. 31. 22:32ㆍAlgorithm/with Java
0. 문제
1. 문제 이해
2. 제출
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static char[] switches;
public static int swi, stu;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
swi = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
switches = new char[swi];
for(int i=0; i<swi; i++) {
switches[i] = st.nextToken().charAt(0);
}
//student
st = new StringTokenizer(br.readLine());
stu = Integer.parseInt(st.nextToken());
for(int i =0; i<stu; i++) {
st = new StringTokenizer(br.readLine());
String gender = st.nextToken();
if(gender.equals("1"))
man(Integer.parseInt(st.nextToken()));
else if(gender.equals("2"))
woman(Integer.parseInt(st.nextToken()));
}
//result
int cnt = 0;
for(char c : switches) {
System.out.print(c+ " ");
cnt++;
if(cnt==20) {
System.out.println();
cnt=0;
}
}
}
public static void man (int idx) {
// System.out.println("man : " + idx);
for(int i=idx; i<=swi; i++) {
if(i%idx==0)
toggle(i-1);
}
}
public static void woman (int idx) {
// System.out.println("woman : " + idx);
int start = idx, end = idx;
for(int i=1; i<swi; i++) {
int left = idx - i;
int right = idx + i;
if(left < 1 || right > swi) break;
if(switches[left -1] != switches[right -1]) break;
start=left; end=right;
}
for(int i=start; i<=end; i++) {
toggle(i-1);
}
}
public static void toggle (int idx) {
if(switches[idx]=='1') {
switches[idx] ='0';
} else if(switches[idx]=='0') {
switches[idx] ='1';
}
// System.out.println("toggle : " + (idx+1) + " -> " + switches[idx]);
}
}
풀어서 통과는 했지만 아쉬운 부분이 보인다.
toggle(i-1);
...
public static void toggle (int idx) {
if(switches[idx]=='1') {
switches[idx] ='0';
} else if(switches[idx]=='0') {
switches[idx] ='1';
}
}
이 부분을 다음과 같이 고칠 수 있다.
switches[i-1] = switches[i-1] == 1 ? 0 : 1;
삼항연산자를 사용하면 더 간단하게 풀 수 있다.
//before
public static void man (int idx) {
for(int i=idx; i<=swi; i++) {
if(i%idx==0)
toggle(i-1);
}
}
//after
public static void man (int idx) {
for(int i=1; i*idx<=swi; i++){
switches[i*idx-1] = switches[i*idx-1] == '1' ? '0' : '1';
}
}
//before
public static void woman (int idx) {
int start = idx, end = idx;
for(int i=1; i<swi; i++) {
int left = idx - i;
int right = idx + i;
if(left < 1 || right > swi) break;
if(switches[left -1] != switches[right -1]) break;
start=left; end=right;
}
for(int i=start; i<=end; i++) {
toggle(i-1);
}
}
//after
public static void woman(int idx){
switches[idx-1] = switches[idx-1] == '1' ? '0' : '1';
for(int start = idx-1, end=idx+1; start>=1 && **e**nd<=swi; start--, end++){
if(switches[start-1] == switches[end-1]){
switches[start-1] = switches[start-1] == '1' ? '0' : '1';
switches[end-1] = switches[end-1] == '1' ? '0' : '1';
}else break;
}
}
3. 최종
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static char[] switches;
public static int swi, stu;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
swi = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
switches = new char[swi+1];
for(int i=1; i<=swi; i++) {
switches[i] = st.nextToken().charAt(0);
}
//student
st = new StringTokenizer(br.readLine());
stu = Integer.parseInt(st.nextToken());
for(int i =0; i<stu; i++) {
st = new StringTokenizer(br.readLine());
String gender = st.nextToken();
if(gender.equals("1"))
man(Integer.parseInt(st.nextToken()));
else if(gender.equals("2"))
woman(Integer.parseInt(st.nextToken()));
}
//result
int cnt = 0;
for(int i=1; i<=swi; i++) {
System.out.print(switches[i]+ " ");
cnt++;
if(cnt==20) {
System.out.println();
cnt=0;
}
}
}
public static void man (int idx) {
for(int i=1; i*idx<=swi; i++){
switches[i*idx] = switches[i*idx] == '1' ? '0' : '1';
}
}
public static void woman(int idx){
switches[idx] = switches[idx] == '1' ? '0' : '1';
for(int start = idx-1, end=idx+1; start>0 && end<=swi; start--, end++){
if(switches[start] == switches[end]){
switches[start] = switches[start] == '1' ? '0' : '1';
switches[end] = switches[end] == '1' ? '0' : '1';
}else break;
}
}
}
switches = new char[swi+1];
: 계속 인덱스마다-1
하기보다는 한 자리를 크게 쓰자.
'Algorithm > with Java' 카테고리의 다른 글
[알고리즘] 풀었던 문제 (240201) (0) | 2024.02.03 |
---|---|
[알고리즘] 15686. 치킨 배달 (0) | 2024.01.31 |
[알고리즘] 2615. 오목 (0) | 2024.01.31 |
[Java] 순열과 조합 (0) | 2024.01.31 |
[알고리즘] 풀었던 문제 (241031) (0) | 2024.01.31 |