728x90

알파벳 소문자로만 이루어진 단어가 주어진다. 이때, 이 단어가 팰린드롬인지 아닌지 확인하는 코드
팰린드롬이란 앞으로 읽을 때와 거꾸로 읽을 때 똑같은 단어를 말한다
level, noon은 팰린드롬이고, baekjoon, online은 팰린드롬이 아니다.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
vector<char> vec_char;
string input;
cin >> input;
copy(input.begin(), input.end(), back_inserter(vec_char));
for (int i = 0; i < vec_char.size(); ++i)
{
if (vec_char.at(i) == vec_char.at(vec_char.size() - 1 - i))
continue;
else
{
cout << "0";
return 0;
}
}
cout << "1";
return 0;
}
- 가장 중요한 부분은 string으로 받아온 input을 copy 함수를 사용해서 vector에 넣는 부분 같다.
- 함수 사용에 익숙하다면 처음과 끝을 계속 비교해가면서 풀면 쉽게 풀 수 있다.
#include <iostream>
using namespace std;
bool isPalindrome(string s){
int len = s.length();
for(int i=0; i < len/2; i++){
if(s[i] != s[len-1-i]) {
return 0;
}
}
return 1;
}
int main(){
ios_base::sync_with_stdio(false); // 두 표준 입출력 동기화 해제
string str;
cin >> str;
cout << isPalindrome(str);
return 0;
}
- 함수를 만들어서 깔끔하게 푸신분도 있었다.
참조 : https://carrot-farmer.tistory.com/26
728x90
'Study > Baekjoon' 카테고리의 다른 글
[백준/C++] 2566 최댓값 (0) | 2024.02.19 |
---|---|
[백준/C++] 10156 과자 (0) | 2024.02.18 |
[백준/C++] 25206 너의 평점은 (0) | 2024.02.17 |
[백준/C++] 1436 영화감독 숌 (0) | 2024.02.17 |
[백준/C++] 2444 별 찍기 - 7 (0) | 2024.02.16 |