[백준] 4. 1차원 배열 풀이

OpenJR·2022년 7월 28일
0

[10818번] 1. 최소, 최대

C++

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);

    int n, a;
    std::vector<int> A;
    std::cin >> n;
    while (std::cin >> a) {
        A.push_back(a);
    }
    int min = *min_element(A.begin(), A.end());
    int max = *max_element(A.begin(), A.end());
    std::cout << min << " " << max;

    return 0;
}

Python

import sys

input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))

Max, Min = max(A), min(A)
print(Min, Max)
    

[2562번] 2. 최댓값

C++

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);

    int a;
    std::vector<int> A;
    while (true) {
        std::cin >> a;
        if (std::cin.eof()) {
            break;
        }
        A.push_back(a);
    }

    int max = *max_element(A.begin(), A.end());
    int max_el = max_element(A.begin(), A.end()) - A.begin() + 1;
    std::cout << max << "\n" << max_el;

    return 0;
}

Python

import sys

input = sys.stdin.readline
A = []
for line in sys.stdin:
    A.append(int(line))

Max = max(A)
Max_index = A.index(Max)
print("{}\n{}".format(Max, Max_index+1))

[2577번] 3. 숫자의 개수

C++

#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);

    int a, num = 1;
    while (true) {
        std::cin >> a;
        if (std::cin.eof()) {
            break;
        }
        num *= a;
    }
    std::string str = std::to_string(num);
    std::vector<int> index(10, 0);

    for (auto i : str) {
        int n = i - '0';
        index[n] += 1;
    }

    for (auto i : index) {
        std::cout << i << "\n";
    }


    return 0;
}

Python

import sys

input = sys.stdin.readline
A = []
for line in sys.stdin:
    A.append(int(line))

num = 1
for a in A:
    num *= a

num_index = [0 for i in range(10)]
for n in str(num):
    num_index[int(n)]+=1

for i in num_index:
    print(i)

[3052번] 4. 나머지

C++

#include <iostream>
#include <vector>
#include <algorithm>

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

    int num;
    std::vector<int> index;
    while (true) {
        std::cin >> num;
        if (std::cin.eof()) {
            break;
        }
        num %= 42;
        if (index.size() == 0) {
            index.push_back(num);
        }

        if (std::find(index.begin(), index.end(), num) != index.end()){
            continue;
        }
        else {
            index.push_back(num);
        }


    }
    std::cout << index.size();

    return 0;
}

Python

import sys

input = sys.stdin.readline
index = list()
for i, line in enumerate(sys.stdin):
    num = int(line) % 42
    
    if i == 0:
        index.append(num)
        
    if num in index:
        continue
    else:
        index.append(num)

print(len(index))

[1546번] 5. 평균

C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int main()
{
    std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);
    std::vector<int> scores;
    int n, score, max_score;
    float sum_score;

    std::cin >> n;

    for (int i =0; i < n; ++i) {
        std::cin >> score;
        scores.push_back(score);
    }
    
    max_score = *std::max_element(scores.begin(), scores.end());
    sum_score = std::accumulate(scores.begin(), scores.end(), 0.0);

    std::cout << sum_score*100/max_score/n;


    return 0;
}

Python

import sys

input = sys.stdin.readline

n = int(input())
scores = list(map(int, input().split()))
max_score = max(scores)

scores = [score*100/max_score for score in scores]
print(sum(scores)/n)

[8958번] 6. OX퀴즈

C++

#include <iostream>
#include <string>

int main()
{
    std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);
    std::string OX;
    char sign = 'X';
    int n, score = 1, sum = 0;
    std::cin >> n;

    for (int i =0; i < n; ++i) {
        std::cin >> OX;
        for (auto ox : OX) {
            if (ox == 'O' && sign == 'O') {
                score++;
            }
            else if (ox == 'X') {
                score = 0;
                sign = 'X';
            }
            else {
                score = 1;
                sign = 'O';
            }
            sum += score;
        }
        std::cout << sum << "\n";
        sum = 0;
        sign = 'X';
    }
    return 0;
}

Python

import sys

input = sys.stdin.readline

n = int(input())
sign = 'X'
sum = 0
for line in sys.stdin:
    for ox in line.strip():
        if ox == 'O' and sign == 'O':
            score += 1
        elif ox == 'X':
            score = 0
            sign = 'X'
        else:
            score = 1
            sign = 'O'
        sum += score
        print("ox: {}, sign: {}, score : {}, sum: {}".format(ox, sign, score, sum))
    print(sum)
    sum, sign = 0, 'X'

[4344번] 7. 평균은 넘겠지

C++

#include <iostream>
#include <vector>
#include <numeric>

int main()
{
    std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);

    int C, n, mean, score;
    std::cin >> C;

    for (int i = 0; i < C; ++i) {
        int cnt = 0;
        std::cin >> n;
        std::vector<int> scores;
        for (int j = 0; j < n; ++j) {
            std::cin >> score;
            scores.push_back(score);
        }
        mean = std::accumulate(scores.begin(), scores.end(), 0.0) / n;

        for (auto scor : scores) {
            if (scor > mean) {
                cnt++;
            }
        }
        float out = (float)cnt/n*100;
        std::cout << std::fixed;
    	std::cout.precision(3);
        std::cout << out << "%\n";
    }

    return 0;
}

Python

import sys

input = sys.stdin.readline

C = int(input())
for i in range(C):
    nscore = list(map(int, input().split()))
    n, score = nscore[0], nscore[1:]
    mean = sum(score)/n
    cnt = len([i for i in score if i > mean])
    print("{:.3f}%".format(round(cnt/n*100, 3)))
profile
Jacob

0개의 댓글