Bibi's DevLog ๐Ÿค“๐ŸŽ

[2021 ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์Šค ํ…Œ์ŠคํŠธ] 1๋‹จ๊ณ„ : ๋‹จ์–ด ๋ฐ€์–ด๋‚ด๊ธฐ ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Java ์ž๋ฐ”

[2021 ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์Šค ํ…Œ์ŠคํŠธ] 1๋‹จ๊ณ„ : ๋‹จ์–ด ๋ฐ€์–ด๋‚ด๊ธฐ

๋น„๋น„ bibi 2020. 12. 14. 17:16
2021 ์ฝ”๋“œ์Šค์ฟผ๋“œ ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์Šค ํ…Œ์ŠคํŠธ ๋ฌธ์ œ์™€ ์ €์˜ ํ’€์ด๋ฅผ ๊ณต์œ ํ•˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค.

 

1๋‹จ๊ณ„: ๋‹จ์–ด ๋ฐ€์–ด๋‚ด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ

  1. ์ž…๋ ฅ: ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ํ•˜๋‚˜, ์ •์ˆ˜ ์ˆซ์ž ํ•˜๋‚˜( -100 <= N < 100) , L ๋˜๋Š” R์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. L ๋˜๋Š” R์€ ๋Œ€์†Œ๋ฌธ์ž ๋ชจ๋‘ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค.
  2. ์ฃผ์–ด์ง„ ๋‹จ์–ด๋ฅผ L์ด๋ฉด ์ฃผ์–ด์ง„ ์ˆซ์ž ๊ฐฏ์ˆ˜๋งŒํผ ์™ผ์ชฝ์œผ๋กœ, R์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋‚ธ๋‹ค.
  3. ๋ฐ€๋ ค๋‚˜๊ฐ„ ๋‹จ์–ด๋Š” ๋ฐ˜๋Œ€์ชฝ์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค.

์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ์˜ˆ์‹œ

ํ™€์ˆ˜ ์ค„์€ ์ž…๋ ฅ, ์ง์ˆ˜ ์ค„์€ ์ถœ๋ ฅ์ด๋‹ค.

> apple 3 L 
leapp

> banana 6 R 
banana 

> carrot -1 r 
arrotc 

> cat -4 R
atc

1๋‹จ๊ณ„ ์ฝ”๋”ฉ ์š”๊ตฌ์‚ฌํ•ญ

  • ์ปดํŒŒ์ผ ๋ฐ ์‹คํ–‰๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ถˆํ•ฉ๊ฒฉ
  • ์ž๊ธฐ๋งŒ์˜ ๊ธฐ์ค€์œผ๋กœ ์ตœ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 


์†Œ์Šค์ฝ”๋“œ

์•„๋ž˜๋Š” ์ œ๊ฐ€ ์ œ์ถœํ•œ ํ’€์ด์ž…๋‹ˆ๋‹ค. 

package Step1;

import java.util.Scanner;

public class Step1 {
    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        Step1 step1 = new Step1();
        step1.starter();
    }

    public void starter() {
        System.out.println("< step1. ๋‹จ์–ด ๋ฐ€์–ด๋‚ด๊ธฐ >");
        String word = inputWord();
        String checkedWord = wordCheck(word);
        int num = inputNum();
        int checkedNum = numCheck(num);
        String LR = inputLR();
        String checkedLR = LRCheck(LR);
        trimInput(checkedWord, checkedNum, checkedLR);
    }

    public String inputWord() {
        System.out.println("๋‹จ์–ด ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
        String word = sc.nextLine();
        return word;
    }

    public String wordCheck(String word) {
        if (word.split(" ").length == 1) {
            return word;
        } else {
            System.out.println("Error : ๋„์–ด์“ฐ๊ธฐ ์—†์ด ํ•œ ๋ฌธ์ž๋กœ ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
            word = inputWord();
            wordCheck(word);
            return word;
        }
    }

    public int inputNum() {
        System.out.println("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. ์ž…๋ ฅํ•œ ์ˆซ์ž๋งŒํผ ๋ฐ€์–ด๋ƒ…๋‹ˆ๋‹ค.");
        System.out.println("-100์ด์ƒ, 100๋ฏธ๋งŒ์˜ ์ •์ˆ˜๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
        int num = sc.nextInt();
        sc.nextLine(); // ๊ฐœํ–‰๋ฌธ์ž ์ œ๊ฑฐ
        return num;
    }

    public int numCheck(int num) {
        if (num >= -100 && num < 100) {
            return num;
        } else {
            System.out.println("Error : -100 ์ด์ƒ 100 ๋ฏธ๋งŒ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”");
            num = inputNum();
            numCheck(num);
            return num;
        }
    }

    public String inputLR() {
        System.out.println("L(l) ๋˜๋Š” R(r)์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\nL ์ž…๋ ฅ์‹œ ์™ผ์ชฝ์œผ๋กœ, R ์ž…๋ ฅ์‹œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋ƒ…๋‹ˆ๋‹ค.");
        String LR = sc.nextLine();
        return LR;
    }

    public String LRCheck(String LR) {
        if (LR.equals("L") || LR.equals("l") || LR.equals("R") || LR.equals("r")) {
            return LR;
        } else {
            System.out.println("Error : L, l (์™ผ์ชฝ) / R, r (์˜ค๋ฅธ์ชฝ) ์ค‘ ํ•˜๋‚˜๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
            LR = inputLR();
            LRCheck(LR);
            return LR;
        }
    }

    public void trimInput(String word, int num, String LR) {
        // LR) ์†Œ๋ฌธ์ž ์ž…๋ ฅ์€ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
        if (LR.equals("l")) LR = "L";
        if (LR.equals("r")) LR = "R";
        // num) ์Œ์ˆ˜๋Š” ์–‘์ˆ˜๋กœ & LR๋ฐ˜๋Œ€๋กœ ๋งŒ๋“ค๊ณ , ๋งŒ๋“ค์–ด์ง„ ์–‘์ˆ˜๋Š” word.length๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋กœ ์น˜ํ™˜
        if (num < 0) {
            if (LR.equals("L")) LR = "R"; // ์™ผ->์˜ค, ์˜ค->์™ผ
            else if (LR.equals("R")) LR = "L";
            num *= -1; // ์–‘์ˆ˜๋กœ ๋งŒ๋“ค๊ธฐ
        }
        int pushNum = num % word.length(); // pushNum = ์‹ค์ œ ๋ฐ€์–ด๋‚ด๋Š” ์ˆซ์ž
        // word) ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
        pushWord(word, pushNum, LR);
    }

    public void pushWord(String word, int num, String LR) {
        String result = null;
        String wordLeft = null;
        String wordRight = null;
        int wordLength = word.length();
        if (LR.equals("L")) { // ์™ผ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋‚ผ ๋•Œ
            // : ์•ž์—์„œ๋ถ€ํ„ฐ num๋ฒˆ์งธ๊นŒ์ง€ ์ž˜๋ผ๋‚ด์–ด(wordRight) ๋‚˜๋จธ์ง€ ๊ธ€์ž(wordLeft)์˜ ๋’ค์— ๋ถ™์ž„.
            wordLeft = word.substring(num, wordLength);
            wordRight = word.substring(0, num);
        }
        if (LR.equals("R")) {// ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋‚ผ ๋–„
            // : ๋’ค์—์„œ๋ถ€ํ„ฐ num๋ฒˆ์งธ๊นŒ์ง€ ์ž˜๋ผ๋‚ด์–ด(wordLeft) ๋‚˜๋จธ์ง€ ๊ธ€์ž(wordRight)์˜ ์•ž์— ๋ถ™์ž„.
            wordLeft = word.substring(wordLength - num, wordLength);
            wordRight = word.substring(0, wordLength - num);
        }
        result = wordLeft + wordRight;
        System.out.println("๊ฒฐ๊ณผ : " + result);
        sc.close();
    }
}

 

์ถœ๋ ฅํ˜•ํƒœ

 


์„ค๋ช…

step-1 branch : Step1.java

1๋‹จ๊ณ„ : ๋‹จ์–ด ๋ฐ€์–ด๋‚ด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ

Step1.java ์ฝ”๋“œ ์„ค๋ช…

  • inputWord() : ๋„์–ด์“ฐ๊ธฐ ์—†๋Š” ํ•œ ๋‹จ์–ด ์ž…๋ ฅ๋ฐ›๊ธฐ

    • wordCheck() : ๋„์–ด์“ฐ๊ธฐ ์žˆ๋Š”์ง€ ํ™•์ธ
  • inputNum() : -100์ด์ƒ 100๋ฏธ๋งŒ์˜ ์ •์ˆ˜.

    • numCheck() : -100์ด์ƒ 100๋ฏธ๋งŒ์ธ์ง€ ํ™•์ธ
  • inputLR() : L,l,R,r ์ค‘ ํ•˜๋‚˜์˜ ๋ฌธ์ž.

    • LRCheck() : L, l, R, r ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฌธ์ž์ธ์ง€ ํ™•์ธ
  • trimInput(word, num, LR) : ๊ฐ ์ž…๋ ฅ๊ฐ’์„ ์ •๋ˆํ•˜๊ธฐ

    • 'word' : ๊ฐ ๊ธ€์ž๋ฅผ ์š”์†Œ๋กœ ํ•˜๋Š” ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
    • 'LR' : l์€ L๋กœ, r์€ R๋กœ ๋ณ€ํ™˜
    • 'num'
      1. ๋งŒ์•ฝ num์ด ์Œ์ˆ˜๋ผ๋ฉด ์–‘์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค, L์€ R๋กœ R์€ L๋กœ ๋ฐ”๊พผ๋‹ค.

      ์ˆซ์ž๊ฐ€ ์Œ์ˆ˜๋ผ๋ฉด, ๊ทธ ์ ˆ๋Œ“๊ฐ’์ธ ์–‘์ˆ˜์˜ ์ˆซ์ž์— L/R์„ ๋ฐ˜๋Œ€๋กœ ํ•œ ๊ฒƒ๊ณผ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™๋‹ค.
      ์˜ˆ๋ฅผ ๋“ค์–ด -1, L (์™ผ์ชฝ์œผ๋กœ -1๊ธ€์ž ๋ฐ€์–ด๋‚ด๊ธฐ)์˜ ๊ฒฐ๊ณผ๋Š” 1,R(์˜ค๋ฅธ์ชฝ์œผ๋กœ 1๊ธ€์ž ๋ฐ€์–ด๋‚ด๊ธฐ)์™€ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™๋‹ค.

      1. num์„ word์˜ ๊ธ€์ž์ˆ˜๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•œ๋‹ค.

      ์ˆซ์ž๊ฐ€ ์•„๋ฌด๋ฆฌ ์ปค๋„, ๊ธ€์ž์ˆ˜๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋งŒํผ๋งŒ ๋ฐ€์–ด๋‚ด๋ฉด ๋œ๋‹ค.
      why? ๋ฐ€๋ ค๋‚˜๊ฐ„ ๋‹จ์–ด๋Š” ๋ฐ˜๋Œ€์ชฝ์œผ๋กœ ์ฑ„์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ.

  • pushWord() : word๋ฅผ num๋งŒํผ L/R๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ€์–ด๋‚ด๊ธฐ

    • (์ด ํ’€์ด๋Š” '์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋‚ด๊ธฐ'๋ณด๋‹ค๋Š” '์ž˜๋ผ์„œ ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ์— ๋ถ™์ด๊ธฐ'์— ๋” ๊ฐ€๊น์Šต๋‹ˆ๋‹ค..)
    • ๊ธ€์ž ์žฌ๋ฐฐ์น˜๋ฅผ ์œ„ํ•ด substring(int beginindex, int endindex)์‚ฌ์šฉ.
    • ์™ผ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋‚ผ ๋•Œ๋Š” ์•ž์—์„œ๋ถ€ํ„ฐ num๋ฒˆ์งธ๊นŒ์ง€ ์ž˜๋ผ๋‚ด์–ด(wordRight) ๋‚˜๋จธ์ง€ ๊ธ€์ž(wordLeft)์˜ ๋’ค์— ๋ถ™์ž„.
      • wordLeft = word.substring(num, wordLength);
      • wordRight = word.substring(0, num);
    • ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋‚ผ ๋•Œ๋Š” ๋’ค์—์„œ๋ถ€ํ„ฐ num๋ฒˆ์งธ๊นŒ์ง€ ์ž˜๋ผ๋‚ด์–ด(wordLeft) ๋‚˜๋จธ์ง€ ๊ธ€์ž(wordRight)์˜ ์•ž์— ๋ถ™์ž„.
      • wordLeft = word.substring(wordLength - num, wordLength);
      • wordRight = word.substring(0, wordLength - num);
      • ๊ฒฐ๊ณผ๋Š” ๋‘˜ ๋‹ค wordLeft์— wordRight๋ฅผ ์ด์–ด๋ถ™์ธ ๊ฒƒ์ด๋‹ค.