Post

[BaekJoon] 14910번 - 오르막 [Java][C++]

[BaekJoon] 14910번 - 오르막 [Java][C++]

문제 링크


1. 문제 풀이


주어진 정수들이 비내림차순인지 판정하는 문제로 이전에 등장한 수를 저장한 변수를 통해 현재 수가 계속 이전 수보다 크거나 같은지 판단하면 된다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int prev = -1_000_000;
        while (st.hasMoreTokens()) {
            int x = Integer.parseInt(st.nextToken());
            if (x >= prev) {
                prev = x;
            } else {
                System.out.println("Bad");
                return;
            }
        }

        System.out.println("Good");
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int prev = -1000000;

    int x;
    while (cin >> x) {
        if (x >= prev) {
            prev = x;
        } else {
            cout << "Bad";
            return 0;
        }
    }

    cout << "Good";
}

This post is licensed under CC BY 4.0 by the author.