๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 10988๋ฒˆ - ํŒฐ๋ฆฐ๋“œ๋กญ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

by dkswnkk 2021. 10. 26.
 

10988๋ฒˆ: ํŒฐ๋ฆฐ๋“œ๋กฌ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

www.acmicpc.net

ํ’€์ด 1

#include <iostream>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    string s; cin >> s;
    bool flag = true;

    if (s.length() % 2 != 0) {    //๊ธธ์ด๊ฐ€ ํ™€์ˆ˜์ผ๋–„
        for (int i = 0; i < s.length() / 2; i++) {
            if (s[i] != s[s.length() - i-1]) flag = false;
        }
    }
    else {    //๊ธธ์ด๊ฐ€ ์ง์ˆ˜์ผ๋•Œ
        if (s[s.length() / 2-1] == s[s.length() / 2]) {
            for (int i = 0; i < s.length() / 2; i++) {
                if (s[i] != s[s.length() - i-1]) flag = false;
            }
        }
        else flag = false;

    }
    if (flag) cout << "1";
    else cout << "0";
}

ํ’€์ด 2

#include <iostream>
using namespace std;

int main() {
    string s; cin >> s;
    int N = s.length();
    int start = 0;
    int end = s.length()-1;
    bool flag = true;
    while (start <= end) {
        if (s[start] != s[end]) flag = false;
        start++;
        end--;
    }
    if (flag) cout << "1";
    else cout << "0";
}

๋Œ“๊ธ€