백준 20115 c++ : Greedy algorithms

magicdrill·2024년 11월 21일

백준 문제풀이

목록 보기
491/673

백준 20115 c++ : Greedy algorithms

오랜만에 c++을 사용해 봤다. 시간이 가능하다면 Java와 함께 풀이해보겠다.
이번주 토익이랑 직무부트캠프 끝나면 좀 시간이 남을거 같다.

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

using namespace std;

void input_data(vector<double>& drink) {
	cout << "input_data()\n";
	int N, i;
	double temp;
	
	cin >> N;
	for (i = 0; i < N; i++) {
		cin >> temp;
		drink.push_back(temp);
	}

	return;
}

double find_answer(vector<double>& drink) {
	cout << "find_answer()\n";
	double max;
	int i;

	//합쳐진 드링크 양을 최대로 하려면?
	//두 개 비교해서 작은거를 반 버리고 큰거에 붓기
	//가장 큰거를 계속 남겨두면 되는거 아님?
	sort(drink.begin(), drink.end());
	max = drink.back();

	for (i = 0; i < drink.size() - 1; i++) {
		max += (drink[i] / 2);
		cout << "current max : " << max << "\n";
	}

	return max;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	vector<double> drink;

	input_data(drink);
	cout << find_answer(drink);

	return 0;
}

0개의 댓글