Post

[BaekJoon] #23037 - 5의 수난 [Java][C++]

[BaekJoon] #23037 - 5의 수난 [Java][C++]

문제 링크


1. 아이디어

주어진 입력을 문자열로 받고 각 자리 수의 다섯 제곱의 합을 구하면 된다.


2. 복잡도

접근시간공간
풀이$O(1)$$O(1)$

(n이 항상 다섯 자리라 s를 순회하는 횟수가 상수)


3. 코드

풀이 [Java][C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String s = br.readLine();
        int sum = 0;

        for (char c : s.toCharArray()) {
            int d = c - '0';
            sum += d * d * d * d * d;
        }

        System.out.println(sum);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    string s;
    cin >> s;

    int sum = 0;
    for (char c : s) {
        int d = c - '0';
        sum += d * d * d * d * d;
    }

    cout << sum;
}

This post is licensed under CC BY 4.0 by the author.