※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 6842번 문제인 Deal or No Deal Calculator이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/6842 

 

6842번: Deal or No Deal Calculator

The user must input a number n (1  n < 10) which indicates how many cases have been opened so far, followed by a list of n integers between 1 and 10 representing the values in the game that have been eliminated, followed by the “Banker’s” offer. For

www.acmicpc.net

문제의 요구에 따라 (남은 briefcase에 든 돈의 총합) / (남은 briefcase의 개수) 과 (deal로 제안된 값)의 대소 비교에 따라 "deal" 또는 "no deal"을 출력하는 프로그램을 작성하자.

 

부동 소수점 오차가 걱정되는 경우, 남은 briefcase의 개수는 항상 양의 정수이므로 위 대소 비교 결과는 (남은 briefcase에 든 돈의 총합)과 (deal로 제안된 값) * (남은 briefcase의 개수)의 대소 비교 결과와 같음을 이용하는 것으로 실수 연산을 피할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

int N, cnt, total = 1691600, deal;
int val[11] = { 0,100,500,1000,5000,10000,25000,50000,100000,500000,1000000 };

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N; cnt = 10 - N;
	while (N--) {
		int x; cin >> x;
		total -= val[x];
	}

	cin >> deal;
	if (deal * cnt > total) cout << "deal";
	else cout << "no deal";
}
728x90

+ Recent posts