Algorithm

Cos Pro 1๊ธ‰ | ๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜

osean 2020. 12. 10. 00:12

๋“ค์–ด๊ฐ€๊ธฐ ์•ž์„œ

๋ชจ๋ฐ”์ผ๋ฆฌ๋” ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•˜๋ฉด์„œ ๋งŒ๋‚œ ๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜๋Š” ๊ต‰์žฅํžˆ ํฅ๋ฏธ๋กœ์šด ๋ฌธ์ œ์˜€๋‹ค.
์–ธ์  ๊ฐ€๋Š” ๊ผญ ๊ธฐ๋กํ•ด์•ผ์ง€ ํ–ˆ๋Š”๋ฐ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ๋ณธ์ง€ 2์ฃผ ํ›„์— ์ด๋ ‡๊ฒŒ ๊ธฐ๋กํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.
๊ทผ๋ฐ ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ด ๋ฌธ์ œ๋ฅผ ์ •์˜ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์„์ง€.. ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.๐Ÿ˜ต

๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜?

 

Narcissistic number - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Integer expressible as the sum of the (number of digits)th power of each of its digits In number theory, a narcissistic number[1][2] (also known as a pluperfect digital invariant (PPDI

en.wikipedia.org

๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜๋ž€ n์˜ ๋ชจ๋“  ์ž๋ฆฌ์ˆ˜์— k ์ œ๊ณฑ์„ ํ•˜๊ณ  ๋”ํ•œ ๊ฐ’์ด n๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ ๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ํ˜น์€ ์ž์•„๋„์ทจ ์ˆ˜.

์šฐ๋ฆฌ๋‚˜๋ผ์—์„œ ๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜ ํ˜น์€ ์ž์•„๋„์ทจ ์ˆ˜๋กœ ๊ตฌ๊ธ€์— ๊ฒ€์ƒ‰ํ•˜๋ฉด ์‹ฌ๋ฆฌํ•™์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ด ๋งŽ์ด ๋‚˜์™€์„œ ์˜์–ด๋กœ ๊ฒ€์ƒ‰ ํ•ด์•ผ ํ•œ๋‹ค.

๋ฌธ์ œ

 

Arori | ๋‹น์‹ ์˜ ์ง€์‹

ํšŒ์›๊ฐ€์ž… ์กฐ๊ฑด์ด ๋งž๋Š”์ง€ ๋‹ค์‹œํ•œ๋ฒˆ ํ™•์ธํ•ด์ฃผ์ƒˆ์š”

www.sysout.co.kr

(ํ”„๋กœ์ ํŠธ๋กœ ์ง„ํ–‰ํ–ˆ๋˜ Arori์— ๋ฌธ์ œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ๊ณต๋ถ€ํ–ˆ๋‹ค. ๊ฝค๋‚˜ ์ž˜ ์‚ฌ์šฉํ•˜๋Š” ์ค‘!)

์–ด๋–ค ์ž๋ฆฌ ์ˆ˜ k๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ k ์ œ๊ณฑ์˜ ํ•ฉ์ด ์›๋ž˜ ์ˆ˜๊ฐ€ ๋˜๋Š” ์ˆ˜๋ฅผ ์ž์•„๋„์ทจ ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 153์€ ์„ธ ์ž๋ฆฌ ์ž์•„๋„์ทจ ์ˆ˜์ž…๋‹ˆ๋‹ค.

http://res.cloudinary.com/drsnvubas/image/upload/c_scale/w_400/v1518153392/narci_qsawna.png

์ž์—ฐ์ˆ˜ k๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ k ์ž๋ฆฌ ์ž์•„๋„์ทจ ์ˆ˜๋“ค์„ ๋ฐฐ์—ด์— ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก solution ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.๋นˆ์นธ์„ ์ฑ„์›Œ ์ „์ฒด ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค๋ช…

k๊ฐ€ solution ๋ฉ”์†Œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

  • k๋Š” 3 ์ด์ƒ 6 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

return ๊ฐ’ ์„ค๋ช…

k ์ž๋ฆฌ ์ž์•„๋„์ทจ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์‹œ

k return
3 [153, 370, 371, 407]

์˜ˆ์‹œ ์„ค๋ช…

  • 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
  • 370 = 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370
  • 371 = 3^3 + 7^3 + 1^1 = 27 + 343 + 1 = 371
  • 407 = 4^3 + 0^3 + 7^3 = 64 + 0 + 343 = 407

ํ’€์ด

์„ค๋ช…

  1. ๋จผ์ € ์ž์—ฐ์ˆ˜ k๋Š” n์˜ ์ž๋ฆฟ์ˆ˜์˜ ์ œ๊ณฑ๊ฐ’์ธ ๊ฒƒ๊ณผ ๋™์‹œ์— n์˜ ์ž๋ฆฟ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ด๋‹ค.
  2. power() ๋ฉ”์†Œ๋“œ๋Š” k ๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ์ž์—ฐ์ˆ˜๋ฅผ ์ œ๊ณฑ๊ฐ’์ด ์–ผ๋งˆ์ธ์ง€ ๊ตฌํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

์ฝ”๋“œ

package ๋ชจ๋ฐ”์ผ๋ฆฌ๋”_์ฝ”๋”ฉํ…Œ์ŠคํŠธ_04;

import java.util.*;

public class question_06 {
    public int power(int base, int exponent) {
        int val = 1;
        for (int i = 0; i < exponent; i++) {
            val *= base;
        }
        return val;
    }

    public int[] solution(int k) {
        // k๋ฅผ ํ†ตํ•ด ์ตœ๋Œ€ ๋ฒ”์œ„๋ฅผ ๊ตฌํ•œ๋‹ค.
        int range = power(10, k);
        int[] answer = new int[range];
        int count = 0;
        // ๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜๋Š” range ๋ฒ”์œ„ ์•ˆ์— ์กด์žฌํ•œ๋‹ค.
        for (int i = range / 10; i < range; i++) {
            int current = i;
            int cal = 0;
            // i์— ๋Œ€ํ•œ ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ k์ œ๊ณฑ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
            // ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ k์ œ๊ณฑ ๊ฐ’์„ ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’ == cal ์ด๋‹ค.
            while(current != 0) {
                cal += power(current % 10, k);
                current /= 10;
            }

            // i์— ๋Œ€ํ•œ ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ k์ œ๊ณฑ ๊ฐ’์„ ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์ด
            // i์™€ ๊ฐ™๋‹ค๋ฉด ๊ทธ ์ˆ˜๋Š” ๋‚˜๋ฅด์‹œ์‹œ์ฆ˜ ์ˆ˜์ด๋‹ค.
            if(cal == i) {
                answer[count] = i;
                count++;
            }
        }
        int[] ret = new int[count];
        for(int i = 0; i < count; i++) {
            ret[i] = answer[i];
        }
        return ret;
    }

    // ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ถœ๋ ฅ์„ ํ•ด๋ณด๊ธฐ ์œ„ํ•œ main ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.
    public static void main(String[] args) {
        question_06 sol = new question_06();
        int k = 3
        int[] ret = sol.solution(k);

        // ์‹คํ–‰] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ถœ๋ ฅ๊ฐ’์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        System.out.printf("solution ๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜ ๊ฐ’์€ ");
        System.out.printf(Arrays.toString(ret));
        System.out.printf(" ์ž…๋‹ˆ๋‹ค.\n");
    }
}