SWEA 2063 중간값 찾기

Jmnote (토론 | 기여)님의 2023년 8월 18일 (금) 17:06 판

1 개요

SWEA 2063 중간값 찾기
  • 난이도: SWEA D1
  • 정렬 후 (N-1)/2 번째 값을 출력한다.
  • N이 홀수이므로 (N-1)/2 번째가 가운데이다.

2 C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int N, temp;
    cin >> N;
    vector<int> a;
    for(int i=0; i<N; i++) {
        cin >> temp;
        a.push_back(temp);
    }
    sort(a.begin(), a.end());
    cout << a[N/2] << endl;
}

3 Java

import java.util.Scanner;
import java.util.Arrays;
class Solution {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int nums[] = new int[N];
        for(int i=0; i<N; i++) nums[i] = sc.nextInt();
        Arrays.sort(nums);
        System.out.println( nums[(N-1)/2] );
    }
}

4 Python

#kcy
k = int(input())
lst = list(map(int, input().split()))
k = int((k-1)/2)
lst.sort()
print(lst[k])
T = int(input())
lst = list(map(int, input().split()))
lst.sort()
print( lst[int((T-1)/2)] )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}