[백준 13771] Presents (C#, C++) - soo:bak
작성일 :
문제 링크
설명
주어진 가격 목록에서 두 번째로 작은 값을 출력하는 문제입니다.
접근법
가격을 센트 단위 정수로 변환해 정렬한 뒤 두 번째 값을 출력합니다.
출력은 원래 형식처럼 소수 둘째 자리까지 맞춥니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var n = int.Parse(Console.ReadLine()!);
var list = new List<int>();
for (var i = 0; i < n; i++) {
var s = Console.ReadLine()!;
var parts = s.Split('.');
var val = int.Parse(parts[0]) * 100 + int.Parse(parts[1]);
list.Add(val);
}
list.Sort();
var ans = list[1];
Console.WriteLine($"{ans / 100}.{ans % 100:D2}");
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector<int> vals;
vals.reserve(n);
for (int i = 0; i < n; i++) {
string s; cin >> s;
int pos = s.find('.');
int val = stoi(s.substr(0, pos)) * 100 + stoi(s.substr(pos + 1, 2));
vals.push_back(val);
}
sort(vals.begin(), vals.end());
int ans = vals[1];
cout << ans / 100 << "." << setw(2) << setfill('0') << ans % 100 << "\n";
return 0;
}