9414 c++ - 프로그래밍 대회 전용 부지

2024. 11. 16. 21:49🐣/BOJ

간단한 정렬 문제이다. (그리디)

pow의 헤더를 포함 안해줘서 컴파일 에러가 났다. cmath를 이용하면 된다.
두번째 틀린 이유는 sum의 오버플로우 이슈 때문이었다.

long long으로 고쳐서 제출하니 통과 되었다.

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

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

#define INIT_MONEY 5 * 1000000
#define FASTIO ios::sync_with_stdio(0), cin.tie(0)

using namespace std;

int main() {
  FASTIO;

  int t;
  cin >> t;

  for (int i = 0; i < t; i++) {
    int m;
    vector<int> p;

    while (1) {
      cin >> m;
      if (m != 0)
        p.push_back(m);
      else
        break;
    }

    sort(p.begin(), p.end(), greater<int>());

    long long sum = 0;
    for (int j = 0; j < p.size(); j++) {
      sum += 2 * pow(p[j], j + 1);
    }
    if (sum > INIT_MONEY) {
      cout << "Too expensive\n";
    } else {
      cout << sum << "\n";
    }
  }
  return 0;
}

 

'🐣 > BOJ' 카테고리의 다른 글

6987 c++ - 월드컵  (0) 2024.11.20
10827 c++ - a^b  (0) 2024.11.17
7453 c++ - 합이 0인 네 정수  (3) 2024.11.15
1655 c++ - 가운데를 말해요  (0) 2024.11.14
30469 c++ - 호반우가 학교에 지각한 이유 2  (1) 2024.11.12