U E D R , A S I H C RSS

Basic알고리즘/팰린드롬/허아영

이야기

오늘 짰던 AOI문제와 비슷해서 쉬웠다.
한글인지 아닌지 판단해서, 구분하는 것도 만들어 볼 생각이다.,

코드

~cpp
#include <iostream>

using namespace std;

bool isPalindrome(char *string, int len)
{	
	bool isPal = false;
	for(int i = 0; i < len/2; i++)
	{
		if(string[i] == string[len-i-1])
			isPal = true;
		else {
			isPal = false;
			break;
		}
	}
	return isPal;
}


int main()
{
	char str[20];
	int len;
	while(cin >> str)
	{
		len = strlen(str);
		if(isPalindrome(str, len))
			cout << "->true" << endl;
		else 
			cout << "->false" << endl;
	}

	return 0;
}


버젼 2. 영어 + 한글 + 버그수정

~cpp
#include <iostream>

using namespace std;

bool isPalindrome(char *string, int len)
{	
	bool isPal = false;
	if(string[0] >= 'a' && string[0] <= 'z' || string[0] >= 'A' && string[0] <= 'Z')
	{
		for(int i = 0; i < len/2; i++)
		{
			if(string[i] == string[len-i-1])
				isPal = true;
			else {
				isPal = false;
				break;
			}
		}
	} else {		
		for(int i = 0; i < (len-1)/2; i += 2)
		{
			for(int j = 0; j < 2; j++)
			{
				if(string[i+j] == string[len-i-2+j])
					isPal = true;
				else {
					isPal = false;
					break;
				}
			}
		}
	}
	return isPal;
}

int main()
{
	char str[20];
	int len;
	while(cin >> str)
	{
		len = strlen(str);
		if(len == 1)
			cout << "->true" << endl;
		else if(isPalindrome(str, len))
			cout << "->true" << endl;
		else 
			cout << "->false" << endl;
	}
	
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:34
Processing time 0.0263 sec