๐Ÿ’ป๋ฌธ์ œ ํ’€์ด - [Java] 2908. ์ƒ์ˆ˜

์ดํ•˜์–€ยท2024๋…„ 1์›” 31์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
3/33
post-thumbnail

๐Ÿ“๋ฌธ์ œ ์ถœ์ฒ˜ : https://www.acmicpc.net/problem/2908
๐Ÿ“code ์ €์žฅ์†Œ
hayannn/CodingTest_Java_NKLCBHub/#2908 ์ƒ์ˆ˜
hayannn/CodingTest_Java_NKLCBHub/#2908 ์ƒ์ˆ˜2
hayannn/CodingTest_Java_NKLCBHub/#2908 ์ƒ์ˆ˜3


๐Ÿค” ๋ฌธ์ œ ์š”์•ฝ

์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

  • ์„ธ ์ž๋ฆฌ์ˆ˜ 2๊ฐœ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๋‘˜ ์ค‘ ํฌ๊ธฐ๊ฐ€ ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
  • ๋‹จ, ์ˆซ์ž ์ธ์‹ ๋ฐฉ์‹์€ ๊ฑฐ๊พธ๋กœ ์ง„ํ–‰ ex)734 893 => 437 398

๐Ÿ“ ์˜ˆ์ œ ์ž…๋ ฅ ๋ถ„์„

  • Case 1 : ๋‘ ์ˆ˜๋ฅผ ๋น„๊ตํ•ด ๋” ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅ ๋•Œ ๋’ค์ง‘์–ด์„œ ๋ณด์—ฌ์ฃผ๊ธฐ

๐Ÿšจ ์ฃผ์˜์‚ฌํ•ญ

  • ๋‘ ์ˆซ์ž์˜ ๊ตฌ๋ถ„์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.
  • ์ˆซ์ž ๋น„๊ต ์ด์ „, ์ˆซ์ž๋ฅผ ๋’ค์ง‘์€ ๋‹ค์Œ์— ๋‘ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.



1๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์ˆซ์ž ๋น„๊ต ๋ฐ ๋’ค์ง‘๊ธฐ

๐Ÿ“๐Ÿ“๊ณต๋ถ€ํ•œ ๋‚ด์šฉ - [Java] 2908. ์ƒ์ˆ˜#์ˆซ์ž ๋’ค์ง‘๊ธฐ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๐Ÿ“ ํ’€์ด ๊ณผ์ • ๋ฐ ์ฝ”๋“œ

  • ๋‘ ์ˆซ์ž๋ฅผ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ์ˆซ์ž ๋’ค์ง‘๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋‘ ์ˆซ์ž ๋’ค์ง‘๊ธฐ
  • ๋’ค์ง‘์€ ๋‘ ์ˆซ์ž ๋น„๊ต ๋ฐ ๋” ํฐ ์ˆซ์ž ์ถœ๋ ฅ
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 1. ์ˆซ์ž ์ž…๋ ฅ๋ฐ›๊ธฐ
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();

        // 2. ์ž…๋ ฅ๋ฐ›์€ ์ฒซ๋ฒˆ์งธ ์ˆซ์ž ๋’ค์ง‘๊ธฐ
        int reverse1 = 0; //num1์„ ๋’ค์ง‘์€ ์ˆซ์ž
        while(num1 != 0) {
            int digit = num1 % 10;
            reverse1 = reverse1 * 10 + digit;
            num1 /= 10;
        }

        // 3. ์ž…๋ ฅ๋ฐ›์€ ๋‘๋ฒˆ์งธ ์ˆซ์ž ๋’ค์ง‘๊ธฐ
        int reverse2 = 0; //num2๋ฅผ ๋’ค์ง‘์€ ์ˆซ์ž
        while(num2 != 0) {
            int digit = num2 % 10;
            reverse2 = reverse2 * 10 + digit;
            num2 /= 10;
        }

        // 4. ๋’ค์ง‘์€ ๋‘ ์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ(์‚ผํ•ญ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ X)
        if(reverse1 > reverse2) {
            System.out.println(reverse1);
        }
        else if(reverse1 < reverse2) {
            System.out.println(reverse2);
        }

        else {
            System.out.print("๋‘ ์ˆ˜๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.");
        }
    }
}






โ™ป๏ธ ๊ฐœ์„  1 - else๋ฌธ ์‚ญ์ œ

  • else๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์ •ํ™•ํžˆ ์–ด๋–ค ๊ฒฝ์šฐ์ธ์ง€๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์–ด๋ ค์› ๋˜ ๊ฒฝํ—˜์ด ๋งŽ์•„ if๋ฌธ์œผ๋กœ ๋Œ€์ฒดํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 1. ์ˆซ์ž ์ž…๋ ฅ๋ฐ›๊ธฐ
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();

        // 2. ์ž…๋ ฅ๋ฐ›์€ ์ฒซ๋ฒˆ์งธ ์ˆซ์ž ๋’ค์ง‘๊ธฐ
        int reverse1 = 0; //num1์„ ๋’ค์ง‘์€ ์ˆซ์ž
        while(num1 != 0) {
            int digit = num1 % 10;
            reverse1 = reverse1 * 10 + digit;
            num1 /= 10;
        }

        // 3. ์ž…๋ ฅ๋ฐ›์€ ๋‘๋ฒˆ์งธ ์ˆซ์ž ๋’ค์ง‘๊ธฐ
        int reverse2 = 0; //num2๋ฅผ ๋’ค์ง‘์€ ์ˆซ์ž
        while(num2 != 0) {
            int digit = num2 % 10;
            reverse2 = reverse2 * 10 + digit;
            num2 /= 10;
        }

        // 4. ๋’ค์ง‘์€ ๋‘ ์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ(์‚ผํ•ญ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ X)
        if(reverse1 > reverse2) {
            System.out.println(reverse1);
        }
        if(reverse1 < reverse2) {
            System.out.println(reverse2);
        }
        if(reverse1 == reverse2) {
            System.out.println("๋‘ ์ˆ˜๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.");
        }
    }
}



โ™ป๏ธ ๊ฐœ์„  2 - ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ

  • ์ˆซ์ž๋ฅผ ๋’ค์ง‘๋Š” ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ฉ”์„œ๋“œ์™€ ๋’ค์ง‘์€ ๋‘ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 1. ์ˆซ์ž ์ž…๋ ฅ๋ฐ›๊ธฐ
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();

        // 2. ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž ๋’ค์ง‘๊ธฐ
        int reverse1 = reverseNumber(num1);
        int reverse2 = reverseNumber(num2);

        // 3. ๋’ค์ง‘์€ ์ˆซ์ž ๋น„๊ตํ•˜๊ณ  ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        Result(reverse1, reverse2);
    }

    // 2' :  ์ˆซ์ž๋ฅผ ๋’ค์ง‘๋Š” ๋ฉ”์†Œ๋“œ
    private static int reverseNumber(int number) {
        int reverse = 0;
        while (number != 0) {
            int digit = number % 10;
            reverse = reverse * 10 + digit;
            number /= 10;
        }
        return reverse;
    }

    // 3' : ๋’ค์ง‘์€ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜๊ณ  ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    private static void Result(int reverse1, int reverse2) {
        // 4. ๋’ค์ง‘์€ ๋‘ ์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ
        if (reverse1 > reverse2) {
            System.out.println(reverse1);
        } else if (reverse1 < reverse2) {
            System.out.println(reverse2);
        } else {
            System.out.println("๋‘ ์ˆ˜๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.");
        }
    }
}

๐Ÿ–จ๏ธ ๊ฒฐ๊ณผ ๋น„๊ต ๋ฐ ๋ถ„์„

๐Ÿ“๐Ÿ“๊ณต๋ถ€ํ•œ ๋‚ด์šฉ - [Java] 2908. ์ƒ์ˆ˜

if - else if - else ๋ฌธ

  • ๋ฉ”๋ชจ๋ฆฌ : 17680KB
  • ์‹œ๊ฐ„ : 208ms
  • ์ฝ”๋“œ ๊ธธ์ด : 1099B

if - return ๋ฌธ

  • ๋ฉ”๋ชจ๋ฆฌ : 17652KB
  • ์‹œ๊ฐ„ : 204ms
  • ์ฝ”๋“œ ๊ธธ์ด : 1115B

๊ธฐ๋Šฅ๋ณ„ ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ

  • ๋ฉ”๋ชจ๋ฆฌ : 17668KB
  • ์‹œ๊ฐ„ : 204ms
  • ์ฝ”๋“œ ๊ธธ์ด : 1211B

โžก๏ธ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ ๋ถ€๋ถ„์—์„œ ๊ฐœ์„ ์„ ํ•˜๊ธด ํ–ˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„์—๋Š” ํฐ ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค๐Ÿฅฒ

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€