Post

[BaekJoon] 2576번 - 홀수 [Java][C++]

[BaekJoon] 2576번 - 홀수 [Java][C++]

문제 링크


1. 문제 풀이


홀수는 $2$ 로 나눈 나머지가 $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
23
24
25
26
import java.io.*;

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

        int sum = 0;
        int min = 100;

        for (int i = 0; i < 7; i++) {
            int n = Integer.parseInt(br.readLine());

            if (n % 2 == 1) {
                sum += n;
                min = Math.min(min, n);
            }
        }

        if (sum == 0) {
            System.out.println(-1);
        } else {
            System.out.println(sum);
            System.out.println(min);
        }
    }
}


2. 풀이 [C++]

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

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

    int sum = 0;
    int mn = 100;

    for (int i = 0; i < 7; i++) {
        int n;
        cin >> n;

        if (n % 2) {
            sum += n;
            mn = min(mn, n);
        }
    }

    if (sum == 0) {
        cout << -1;
    } else {
        cout << sum << '\n';
        cout << mn << '\n';
    }
}

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