> >
본문 바로가기

Algorithm/BOJ

[BOJ] 15276번 Palindrmoic Password

#include <bits/stdc++.h>
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;

int main(){
    FASTIO;
    
    string tmp(6, ' ');
    vector<int> res;
    for (int i = 1; i <= 9; i++){
        for (int j = 0; j <= 9; j++){
            for (int k = 0; k <= 9; k++){
                tmp[0] = tmp[5] = i + '0';
                tmp[1] = tmp[4] = j + '0';
                tmp[2] = tmp[3] = k + '0';
                res.push_back(stoi(tmp));
            }
        }
    }
    
    int n; cin >> n;
    while(n--){
        string s, t; cin >> s;
        int i = stoi(s);
        int min = 1e9;
        for (auto it : res){
            if (min > abs(i - it)){
                min = abs(i - it);
                t = to_string(it);
            }
        }
        cout << t << endl;
    }
    return 0;
}

 

주어지는 수와 가장 차이가 적은 회문을 구해야 하는 문제입니다. 10^3만 전처리해주면 쉽게 구할 수 있습니다.

'Algorithm > BOJ' 카테고리의 다른 글

[BOJ] 18115번 카드 놓기  (0) 2024.07.10
[BOJ] 31802번 주기 함수 (Easy)  (0) 2024.07.10
[BOJ] 30445번 행복 점수  (0) 2024.07.10
[BOJ] 14503번 로봇 청소기  (0) 2024.07.09
[BOJ] 2296번 건물짓기  (0) 2024.07.08