File tree Expand file tree Collapse file tree 1 file changed +77
-0
lines changed
Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+
4+ public class Main {
5+
6+ static int N ;
7+ static char [] arr;
8+ public static void main (String [] args ) throws IOException {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+
12+ N = Integer . parseInt(br. readLine());
13+ arr = new char [N ];
14+ for (int i = 0 ; i < N ; i++ ) {
15+ arr[i] = br. readLine(). charAt(0 );
16+ }
17+
18+ char [] result = new char [N ];
19+ int headIdx = 0 ; // arr 앞쪽 포인터
20+ int tailIdx = N - 1 ; // arr 뒤쪽 포인터
21+ int idx = 0 ; // result의 인덱스
22+
23+ while (headIdx <= tailIdx) {
24+
25+ if (idx == N ) { break ; }
26+
27+ if (arr[headIdx] < arr[tailIdx]) {
28+ result[idx++ ] = arr[headIdx++ ];
29+ }
30+ else if (arr[headIdx] > arr[tailIdx]) {
31+ result[idx++ ] = arr[tailIdx-- ];
32+ }
33+ else {
34+
35+ boolean isHeadSmaller = true ;
36+
37+ for (int offset = 1 ; offset < N ; offset++ ) {
38+ if (headIdx + offset >= N || tailIdx - offset < 0 ) {
39+ break ;
40+ }
41+ if (headIdx + offset > tailIdx - offset) {
42+ break ;
43+ }
44+
45+ if (arr[headIdx + offset] < arr[tailIdx - offset]) {
46+ break ;
47+ } else if (arr[headIdx + offset] > arr[tailIdx - offset]) {
48+ isHeadSmaller = false ;
49+ break ;
50+ }
51+ }
52+
53+ if (isHeadSmaller) {
54+ result[idx++ ] = arr[headIdx++ ];
55+ } else {
56+ result[idx++ ] = arr[tailIdx-- ];
57+ }
58+ }
59+ }
60+
61+
62+ // 80글자마다 새줄 문자를 출력한다.
63+ for (int i = 0 ; i < N ; i++ ) {
64+ bw. write(result[i]);
65+ if ((i+ 1 ) % 80 == 0 ) {
66+ bw. write(" \n " );
67+ }
68+ }
69+
70+ br. close();
71+ bw. flush();
72+ bw. close();
73+ }
74+
75+ }
76+
77+ ```
You can’t perform that action at this time.
0 commit comments