피너클의 it공부방
백준 1259 팰린드롬수 (c++) : 피너클 본문
728x90
반응형
https://www.acmicpc.net/problem/1259
1259번: 팰린드롬수
입력은 여러 개의 테스트 케이스로 이루어져 있으며, 각 줄마다 1 이상 99999 이하의 정수가 주어진다. 입력의 마지막 줄에는 0이 주어지며, 이 줄은 문제에 포함되지 않는다.
www.acmicpc.net
간단한 구현문제다.
string s;
while (true) {
cin >> s;
if (s == "0") break;
if (chk(s)) cout << "yes" << endl;
else cout << "no" << endl;
}
string s를 선언한뒤 반복문을 true로 설정한다.
s를 입력받은 다음 s가 0이면 반복문을 빠져나온다.
만약 chk(s)가 true라면 yes를, false라면 no를 출력한다.
bool chk(string s) {
for (int i = 0; i < s.length(); i++) {
if (i >= s.length() - 1 - i) break;
if (s[i] != s[s.length() - 1 - i]) return false;
}
return true;
}
함수 chk는 string s를 전달받는다.
s의 길이만큼의 반복문을 설정하지만 만약 int i가 s.length() - 1 - i보다 크거나 같다면 반복문을 빠져나온다.
그리고 만약 s[i]와 s[s.length() - 1 - i]가 다르다면 바로 false를 리턴하고 반복문이 끝난다면 true를 리턴한다.
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
bool chk(string s) {
for (int i = 0; i < s.length(); i++) {
if (i >= s.length() - 1 - i) break;
if (s[i] != s[s.length() - 1 - i]) return false;
}
return true;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
string s;
while (true) {
cin >> s;
if (s == "0") break;
if (chk(s)) cout << "yes" << endl;
else cout << "no" << endl;
}
}
전체코드다.
728x90
반응형
'백준' 카테고리의 다른 글
백준 11660 구간 합 구하기 5 (c++) : 피너클 (0) | 2022.05.05 |
---|---|
백준 1268 임시 반장 정하기 (c++) : 피너클 (0) | 2022.05.04 |
백준 1039 교환 (c++) : 피너클 (0) | 2022.04.29 |
백준 1331 나이트 투어 (c++) : 피너클 (0) | 2022.04.28 |
백준 1251 단어 나누기 (c++) : 피너클 (0) | 2022.04.27 |
Comments