전공/Problem Solving

[알고리즘/문제풀이/BOJ 1259번] 팰린드롬수

caneo 2021. 7. 18. 01:34
728x90
반응형

팰린드롬수 문제는 문자열을 다루는 문제이다. 문자열을 대칭적으로 확인하여 같지 않으면 no를 출력하고, 전부 같으면 yes를 출력하도록 함수를 작성하면 된다.

작성한 코드는 다음과 같다.


#include<stdio.h>
#include<string.h>

char temp[10];

int func() {
	int length = strlen(temp);
	if (length == 1) {
		if(temp[0] == '0')
			return -1;
		else {
			return 1;
		}
	}
	else {
		for (int i = 0;i<=length/2; i++) {
			if (temp[i] != temp[length - 1 - i]) {
				return 0;
			}
		}

		return 1;
	}
}

int main(void) {
	while (1) {
		int inst;
		scanf("%s", temp);
		inst = func();
		if (inst == -1) break;
		else if (inst == 0) printf("no\n");
		else printf("yes\n");
	}
}

문제의 지문은 다음의 링크에서 확인할 수 있다.

https://www.acmicpc.net/problem/1259

728x90
반응형