Algorithm/BOJ

[BOJ 11536] S5 줄세우기 { C++, Java, Python, Kotlin }

surimi🍥 2022. 12. 17. 19:46
반응형

11536번: 줄 세우기

C++

#include <iostream>

using namespace std;

// s1이 작으면 음수
int cmp(string s1, string s2)
{
    int len = (s1.length() > s2.length()) ? s2.length() : s1.length();
    for (int i = 0; i < len; i++)
    {
        if (s1[i] != s2[i])
            return (s1[i] - s2[i]);
    }
    if ((s1.length() != s2.length()) && (len == s1.length()))
        return (-1);
    return (0);
}

int main(void)
{
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);

    // 지속 상승, 하락 flag 설정
    bool inc = 1, dec = 1;
    int T;
    cin >> T;
    string S[T];
    cin >> S[0];
    for (int i = 1; i < T; i++)
    {
        cin >> S[i];
         // 앞이랑 지금이랑 비교해서 앞이 크면 increasing이 아님.
        if (cmp(S[i - 1], S[i]) > 0)
            inc = 0;
         // 앞이랑 지금이랑 비교해서 지금이 크면 decreasing이 아님.
        if (cmp(S[i - 1], S[i]) < 0)
            dec = 0;
    }
    if (inc)
        cout << "INCREASING";
    else if (dec)
        cout << "DECREASING";
    else
        cout << "NEITHER";
}

Java

import java.io.BufferedReader;
import java.io.InputStreamReader;

class _11536 {
    public void solution() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            boolean inc = true, dec = true;
            int T = Integer.parseInt(br.readLine().trim());
            String[] s = new String[T];
            s[0] = br.readLine();
            for (int i = 1; i < T; i++)
            {
                s[i] = br.readLine();
                if (s[i-1].compareTo(s[i]) < 0) dec = false; 
                if (s[i-1].compareTo(s[i]) > 0) inc = false; 
            }
            
            if (inc)
                System.out.println("INCREASING");
            else if (dec)
                System.out.println("DECREASING");
            else
                System.out.println("NEITHER");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Main {
    public static void main(String[] args) {
        new _11536().solution();
    }
}

Kotlin

import java.io.BufferedReader
import java.io.InputStreamReader

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val T = Integer.parseInt(br.readLine());
    val arr = Array(T) { "" }
    var inc = 1
    var dec = 1
    arr[0] = br.readLine()
    for (i in 1 until T) {
        arr[i] = br.readLine()
        if (arr[i - 1] > arr[i]) inc = 0
        if (arr[i - 1] < arr[i]) dec = 0
    }
    if (inc == 1) {
        println("INCREASING")
    } else if (dec == 1) {
        println("DECREASING")
    } else {
        println("NEITHER")
    }
}

Python

from sys import stdin
input = stdin.readline

def main():
    T = int(input())

    # T번 문자열 입력 받아서 trim
    arr = [input().strip() for _ in range(T)]
    inc, dec = 1, 1
    for i in range(1, T):
        if arr[i-1] > arr[i]: inc = 0
        if arr[i-1] < arr[i]: dec = 0
    if inc: print("INCREASING")
    elif dec: print("DECREASING")
    else: print("NEITHER")

if __name__ == '__main__':
    main()
반응형