05-05 05:52
Notice
Recent Posts
Recent Comments
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

(주) 망나니 힘집

[해커랭크] 코딩테스트 문제 시도 [Java Output Formatting] [10 포인트] 본문

스펙도 쌓니/코테 막푸니

[해커랭크] 코딩테스트 문제 시도 [Java Output Formatting] [10 포인트]

군포망나니 2024. 11. 6. 01:00

[해커랭크] 코딩테스트 문제 시도 [nn번] [해결 =? O/X]

"풀이" 가 아닌 "시도" 를 추구합니다.

 

풀이 환경  

 

 

언어 버전 : Java8 

사용 패키지 : x    

문제 요약   

 

 

예... 뭐.... 

나의 코드 및 나의 해설 

 

일단 뭐 첨 봤을 때는 그냥 적당히 간격을 주고 \t 이런 거 주면 되는 거 아니냐는 생각을 했었다. 아아 이스케이프 문자구나

아니면 막 % 로 자릿수 맞추는 것도 있지 않았나? 일단 기본기 부족 확인. 

 

결국 이스케이프 문자와 자릿수 맞추는 것 관련해서 구글링을 해본다. 

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("================================");
            for(int i=0;i<3;i++)
            {
                String s1=sc.next();
                int x=sc.nextInt();
                
                //Complete this line
                System.out.print(s1 + "\t");
                System.out.print(x + "\n");
               
            }
            System.out.println("================================");

    }
}

 

요렇게 맞춰줬다. 

 

응 아니야 

 

% 를 사용해보기로 했다. 

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("================================");
            for(int i=0;i<3;i++)
            {
                String s1=sc.next();
                int x=sc.nextInt();
                
                //Complete this line
                // System.out.print(s1 + "\t");
                // System.out.print(String.format("%03d", x) + "\n");
               System.out.printf("%15s \t %03d \n", s1, x );
            }
            System.out.println("================================");

    }
}

 

오 그럴 듯 하다 과연? 

 

거의 다 온 것 같은 느낌 (초긍정) 

일단 문자열 자릿수 최대 15자리라길래 %15s 를 넣어줬지만 왼쪽부터 공백을 채운 것 같다. 흠 왼쪽에 문자열을 배치하는 방법이 없을까? 

 

 System.out.printf("%-15s \t %03d \n", s1, x );

 

숫자 앞에 - 붙이니 왼쪽으로 정렬이 됨. 하지만 fail ~ 왜...??

 

아니 진짜 왜

 

꺼진 불도 다시 보자

 

\t 왜 그대로 둔 거임 자릿수를 맞춰서 필요가 없자나 

 

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("================================");
            for(int i=0;i<3;i++)
            {
                String s1=sc.next();
                int x=sc.nextInt();
       
               System.out.printf("%-15s%03d\n", s1, x );
            }
            System.out.println("================================");

    }
}

 

깔꼼쓰 ~

문제 해답 풀이와 비교  

 

다른 사람들이 푼 코드를 구경했는데 상당히 복잡한 코드도 많아서 생략 

그리고 의외로 

if(x>0 && x<999 && s1.length()>10)

 

이 조건을 넣어준 사람이 많다. 실무에서는 제법 유용할지도 ? 

시간복잡도 및 공간복잡도 비교 

 

x

 

놓친 부분과 배운 개념

 

printf 형식

"%3s" "%-3s" : 오른쪽 정렬 / 왼쪽 정렬 

"%4d" "%-5d" : 뒤에 공백을 채움 / 앞에 0을 채움