Notice
Recent Posts
Recent Comments
Link
250x250
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

피너클의 it공부방

백준 1259 팰린드롬수 (c++) : 피너클 본문

백준

백준 1259 팰린드롬수 (c++) : 피너클

피너클 2022. 5. 2. 16:53
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
반응형
Comments