왜 안 되는지 계속 찾다가 boolean 로 되는 경우 안되는 경우 flag지정해서 출력 여부 케이스를 나웠는데.. 안되고
boolean없이 reaturn할 때는 되고..
안 되는 이유가 있을까요..
안되는 코드입니다
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>));
int n = Integer.<i>parseInt</i>(br.readLine());
int[] arr = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
arr[i] = Integer.<i>parseInt</i>(br.readLine());
}
boolean result = true;
int count = 0;
int now = 0;
int p;
StringBuilder builder = new StringBuilder();
while (count < n) {
p = arr[count];
if (p > now) {
for (int i = now + 1; i <= p; i++) {
stack.push(i);
builder.append("+\n");
}
stack.pop();
builder.append("-\n");
now = p;
} else {
if (stack.peek() == p) {
stack.pop();
builder.append("-\n");
} else {
System.<i>out</i>.println("NO");
result = false;
}
}
count++;
}
if(result) {
builder.deleteCharAt(builder.length() - 1);
System.<i>out</i>.println(builder);
}
}
}
아래는 되는 코드입니다
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>));
int n = Integer.<i>parseInt</i>(br.readLine());
int[] arr = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
arr[i] = Integer.<i>parseInt</i>(br.readLine());
}
boolean result = true;
int count = 0;
int now = 0;
int p;
StringBuilder builder = new StringBuilder();
while (count < n) {
p = arr[count];
if (p > now) {
for (int i = now + 1; i <= p; i++) {
stack.push(i);
builder.append("+\n");
}
stack.pop();
builder.append("-\n");
now = p;
} else {
if (stack.peek() == p) {
stack.pop();
builder.append("-\n");
} else {
System.<i>out</i>.println("NO");
return;
}
}
count++;
}
builder.deleteCharAt(builder.length() - 1);
System.<i>out</i>.println(builder);
}
}
treesheep 3년 전
왜 안 되는지 계속 찾다가 boolean 로 되는 경우 안되는 경우 flag지정해서 출력 여부 케이스를 나웠는데.. 안되고
boolean없이 reaturn할 때는 되고..
안 되는 이유가 있을까요..
안되는 코드입니다
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>)); int n = Integer.<i>parseInt</i>(br.readLine()); int[] arr = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < n; i++) { arr[i] = Integer.<i>parseInt</i>(br.readLine()); } boolean result = true; int count = 0; int now = 0; int p; StringBuilder builder = new StringBuilder(); while (count < n) { p = arr[count]; if (p > now) { for (int i = now + 1; i <= p; i++) { stack.push(i); builder.append("+\n"); } stack.pop(); builder.append("-\n"); now = p; } else { if (stack.peek() == p) { stack.pop(); builder.append("-\n"); } else { System.<i>out</i>.println("NO"); result = false; } } count++; } if(result) { builder.deleteCharAt(builder.length() - 1); System.<i>out</i>.println(builder); } } }아래는 되는 코드입니다
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>)); int n = Integer.<i>parseInt</i>(br.readLine()); int[] arr = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < n; i++) { arr[i] = Integer.<i>parseInt</i>(br.readLine()); } boolean result = true; int count = 0; int now = 0; int p; StringBuilder builder = new StringBuilder(); while (count < n) { p = arr[count]; if (p > now) { for (int i = now + 1; i <= p; i++) { stack.push(i); builder.append("+\n"); } stack.pop(); builder.append("-\n"); now = p; } else { if (stack.peek() == p) { stack.pop(); builder.append("-\n"); } else { System.<i>out</i>.println("NO"); return; } } count++; } builder.deleteCharAt(builder.length() - 1); System.<i>out</i>.println(builder); } }