문제 링크입니다: https://www.acmicpc.net/problem/11068
11068번: 회문인 수
문제 어떤 수를 왼쪽부터 읽어도, 오른쪽부터 읽어도 같을 때 이 수를 회문인 수라고 한다. 예를 들어, 747은 회문인 수이다. 255도 회문인 수인데, 16진수로 표현하면 FF이기 때문이다. 양의 정수를 입력받았을 때, 이 수가 어떤 B진법 (2 ≤ B ≤ 64)으로 표현하면 회문이 되는 경우가 있는지 알려주는 프로그램을 작성하시오. B진법이란, 한 자리에서 수를 표현할 때 쓸 수 있는 수의 가짓수가 B라는 뜻이다. 예를 들어, 십진법에서 B는 10이다
www.acmicpc.net
B가 최대 64이기 때문에 63까지 일일히 문자를 매칭시켜주기보다는 주어진 N을 B진수로 만들고 거꾸로 뒤집었을 때 같은 숫자인지 판단하는 방식으로 접근해줬습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
const int MIN = 2; | |
const int MAX = 64; | |
bool isPalindrome(int N) | |
{ | |
for (int b = MIN;b <= MAX; b++) | |
{ | |
int num = N; | |
string s; | |
while (num) | |
{ | |
s.push_back(num%b); | |
num /= b; | |
} | |
string reverseS; | |
for (int i = s.length() - 1; i >= 0; i--) | |
{ | |
reverseS += s[i]; | |
} | |
if (s == reverseS) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int T; | |
cin >> T; | |
for (int t = 0; t < T; t++) | |
{ | |
int N; | |
cin >> N; | |
cout << isPalindrome(N) << "\n"; | |
} | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 17362번 수학은 체육과목 입니다 2 (0) | 2020.03.21 |
---|---|
백준 4889번 안정적인 문자열 (0) | 2020.03.21 |
백준 2525번 오븐 시계 (0) | 2020.03.13 |
백준 18258번 큐 2 (0) | 2020.03.11 |
백준 6198번 옥상 정원 꾸미기 (0) | 2020.03.08 |