문제 바로가기
접근방법
- 가장 큰 값을 찾아 -1을 해주고 가장 작은 값을 찾아 +1을 해줘야 한다.
- 가장 쉬운 방법은 정렬을 하여 처음 값(가장 작은 값)+1, 마지막 값(가장 큰 값)-1 해주는 것이다.
- +1, -1을 하고 나면 큰 값과 작은 값이 달라 질 수 있어 계속 정렬을 해줘야 한다.
풀이
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#define width 100
using namespace std;
int test_case, T = 10, dump, a, answer;
vector<int> v;
void inputHeight() {
for (int i = 0; i < width; i++) {
cin >> a;
v.push_back(a);
}
}
void flattering() {
answer = 0;
for (int i = 0; i < dump; i++) {
v[0] += 1;
v[width - 1] -= 1;
sort(v.begin(), v.end());
}
answer = v[width - 1] - v[0];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
for (test_case = 1; test_case <= T; test_case++) {
cin >> dump;
inputHeight();
sort(v.begin(), v.end());
flattering();
cout << "#" << test_case << " " << answer << "\n";
v.clear();
}
return 0;
}