๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

[๋ฐฑ์ค€/Java] 2588 ๊ณฑ์…ˆ ๋ฌธ์ œ ํ’€์ด (์ž๋ฆฟ์ˆ˜ ๋ถ„๋ฆฌํ•˜๊ธฐ)

by ๋ฝ€์งœ๊ผฌ 2025. 1. 14.
728x90
๋ฐ˜์‘ํ˜•

์•„๋‹ˆ ์ด ๋ฌธ์ œ์— ์‚ฌ์ง„์ด ์žˆ๋‹ค๋Š”๊ฑธ ๋ชฐ๋ž๋‹ค..

์ฒ˜์Œ์— ์‚ฌ์ง„์ด ์•”๊ฒƒ๋‘ ์•ˆ๋œจ๊ธธ๋ž˜ ๊ทธ๋ƒฅ ๊ฐ์œผ๋กœ (1),(2),(3),(4),(5)๋Š” ์ด๊ฑฐ๊ฒ ์ง€..? ํ•˜๊ณ  ํ’€์—ˆ๋Š”๋ฐ

๋‚˜์ค‘์—์•ผ ๊ทธ๋ฆผ ๋œธ.. ์–ต์šธํ•‘.

์–ต์šธํ–‰


๋ฌธ์ œ

(์„ธ ์ž๋ฆฌ ์ˆ˜) × (์„ธ ์ž๋ฆฌ ์ˆ˜)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•˜์—ฌ ์ด๋ฃจ์–ด์ง„๋‹ค.

(1)๊ณผ (2)์œ„์น˜์— ๋“ค์–ด๊ฐˆ ์„ธ ์ž๋ฆฌ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ (3), (4), (5), (6)์œ„์น˜์— ๋“ค์–ด๊ฐˆ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— (1)์˜ ์œ„์น˜์— ๋“ค์–ด๊ฐˆ ์„ธ ์ž๋ฆฌ ์ž์—ฐ์ˆ˜๊ฐ€, ๋‘˜์งธ ์ค„์— (2)์˜ ์œ„์น˜์— ๋“ค์–ด๊ฐˆ ์„ธ์ž๋ฆฌ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

472
385

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ ๋„ท์งธ ์ค„๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ (3), (4), (5), (6)์— ๋“ค์–ด๊ฐˆ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

2360
3776
1416
181720

๐Ÿ’ญ ๋ฌธ์ œ ํ’€์ด ์ƒ๊ฐ

์šฐ์„  3,4,5๋Š” (2)์˜ ์ˆซ์ž๋ฅผ ํ•œ์ž๋ฆฌ ์ˆซ์ž์”ฉ ๋ถ„๋ฆฌ๋ฅผ ํ•ด์„œ ๊ณฑํ•œ๊ฒƒ์ด๋‹ค.

(3)์€ 472 * 5

(4)๋Š” 472 * 8

...

๊ทธ๋ž˜์„œ ํ•œ์ž๋ฆฌ์”ฉ ๋ถ„๋ฆฌํ•ด์„œ ๊ณฑํ•œ๋‹ค์Œ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ฒ ๋‹ค ์ƒ๊ฐํ–ˆ๋‹ค.

 

๐Ÿ’ก ๊ฐ๊ฐ์˜ ์ž๋ฆฟ์ˆ˜ ๋ถ„๋ฆฌํ•˜๊ธฐ

while( second > 0) {
  System.out.println(first * (second % 10));
  resultToken.add(first * (second % 10));
  second /= 10;
}

 

์ž๋ฆฟ์ˆ˜๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‚˜๋ˆ—์…ˆ์„ ์ด์šฉํ–ˆ๋‹ค.

% 10๊ณผ์˜ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ์ด์šฉํ•ด ์ผ์˜์ž๋ฆฌ๋ฅผ ์ถ”์ถœํ•˜๊ณ ,

/ 10๊ณผ์˜ ๋‚˜๋ˆ—์…ˆ์„ ์ด์šฉํ•ด ๋งˆ์ง€๋ง‰ ์ž๋ฆฟ ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

int number = 12345;

while (number > 0) {
    int d = number % 10; // ๋งˆ์ง€๋ง‰ ์ž๋ฆฟ์ˆ˜ ์ถ”์ถœ
    number /= 10; // ๋งˆ์ง€๋ง‰ ์ž๋ฆฟ์ˆ˜๋ฅผ ์ œ๊ฑฐ
}

// 5 4 3 2 1

๐Ÿ’ก ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ

(6)๋ฒˆ์€ (3),(4),(5)๋ฅผ ํ•œ์นธ์”ฉ ๋ฐ€์–ด์„œ ๋”ํ•œ ๋Š๋‚Œ์ด์—ˆ๋‹ค.

๊ทธ๋ƒฅ ๊ฐ„๋‹จํ•˜๊ฒŒ

์•„ ๋’ค์— 0์„ ์ถ”๊ฐ€ํ•ด์„œ ๋”ํ•˜๋ฉด ๋˜๊ฒ ๋‹ค ์‹ถ์—ˆ๋‹ค.

for ( int i = 0; i < resultToken.size(); i++) {
            result += (resultToken.get(i) * (int)Math.pow(10, i));
        }
        System.out.println(result);

 

๊ทธ๋ž˜์„œ for๋ฌธ์„ ๋Œ๋ฆฌ๋ฉด์„œ, ์ธ๋ฑ์Šค ๊ฐ’๋งŒํผ 10์˜ ์ œ๊ณฑ์„ ๊ณฑํ•ด์ฃผ์—ˆ๋‹ค!

 

๊ทธ๋ฆฌํ•˜์—ฌ ํ•ด๊ฒฐ์™„๋ฃŒ

 

โœ๏ธ ์ž‘์„ฑ ๋‹ต์•ˆ

import java.util.ArrayList;
import java.util.Scanner;
import java.lang.Math;

public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int first = scanner.nextInt();
        int second = scanner.nextInt();
        int result = 0;

        ArrayList<Integer> resultToken = new ArrayList<>();
        while( second > 0) {
            System.out.println(first * (second % 10));
            resultToken.add(first * (second % 10));
            second /= 10;
        }

        for ( int i = 0; i < resultToken.size(); i++) {
            result += (resultToken.get(i) * (int)Math.pow(10, i));
        }
        System.out.println(result);

    }
}

 

728x90
๋ฐ˜์‘ํ˜•