Algorithms/BaekJoon
[백준] 입출력 - (Java) 10171번 고양이 && 10172번 개
Jjiveloper
2022. 6. 6. 00:05
728x90
10171번
제출 1
public class Main{
public static void main(String[] args){
int maxLength = " ) ( ')".length();
String[] cat = {
"\\ /\\",
" ) ( ')",
"( / )",
" \\(__)|"
};
for (int i = 0; i < cat.length; i++) {
System.out.println(String.format("%-" + maxLength + "s", cat[i]));
}
}
}
출처: https://www.acmicpc.net/problem/10171
10171번: 고양이
아래 예제와 같이 고양이를 출력하시오.
www.acmicpc.net
10172번
제출 1
public class Main {
public static void main(String[] args){
StringBuilder sb = new StringBuilder();
sb.append("|\\_/|").append("\n")
.append("|q p| /}").append("\n")
.append("( 0 )\"\"\"\\").append("\n")
.append("|\"^\"` |").append("\n")
.append("||_/=\\\\__|");
System.out.println(sb);
}
}
제출 2
public class Main {
public static void main(String[] args){
StringBuilder sb = new StringBuilder();
sb.append("|\\_/|\n" +
"|q p| /}\n" +
"( 0 )\"\"\"\\\n" +
"|\"^\"` |\n" +
"||_/=\\\\__|");
System.out.println(sb);
}
}
성능: 한번에 다 append < 여러번 append
제출 3
public class Main {
public static void main(String[] args){
System.out.print(
"|\\_/|\n" +
"|q p| /}\n" +
"( 0 )\"\"\"\\\n" +
"|\"^\"` |\n" +
"||_/=\\\\__|"
);
}
}
제출 4
public class Main {
public static void main(String[] args){
System.out.println("|\\_/|");
System.out.println("|q p| /}");
System.out.println("( 0 )\"\"\"\\");
System.out.println("|\"^\"` |");
System.out.print("||_/=\\\\__|");
}
}
성능: 여러번 println < 한번에 print
반복문이 아닌 단일문에서는 StringBuilder보다 일반 print문이 성능이 더 좋은듯...? 넘 미묘한 차이긴 하지만
왜 그런지 찾아봤는데 아직 정확한 이유를 찾지 못했다...
궁금하니 나중에 더 찾아봐야겠다.
출처: https://www.acmicpc.net/problem/10172
10172번: 개
문제 아래 예제와 같이 개를 출력하시오. 입력 출력 예제 입력 1 복사 예제 출력 1 복사 |\_/| |q p| /} ( 0 )"""\ |"^"` | ||_/=\\__|...
www.acmicpc.net
728x90
반응형