작성일 :

문제 링크

9325번 - 얼마?

설명

자동차의 기본 가격옵션 수와 각 옵션의 수량 및 단가가 주어졌을 때,
최종 차량 가격을 계산하는 간단한 누적 합산 문제입니다.


접근법

  • 테스트할 차량 수가 주어집니다.
  • 각 차량에 대해:
    • 기본 차량 가격을 입력받고,
    • 옵션 개수를 입력받은 뒤, 각 옵션마다 수량과 단가를 입력받습니다.
    • 각 옵션 가격은 수량 × 단가이며, 이들의 합을 기본 가격에 더합니다.
  • 차량별로 최종 가격을 출력합니다.

Code

[ C# ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;

namespace Solution {
  class Program {
    static void Main(string[] args) {
      int t = int.Parse(Console.ReadLine()!);
      while (t-- > 0) {
        long cost = long.Parse(Console.ReadLine()!);
        int cntOpt = int.Parse(Console.ReadLine()!);
        for (int i = 0; i < cntOpt; i++) {
          var input = Console.ReadLine()!.Split();
          int qty = int.Parse(input[0]);
          int price = int.Parse(input[1]);
          cost += qty * price;
        }
        Console.WriteLine(cost);
      }
    }
  }
}



[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

  int t; cin >> t;
  while (t--) {
    ll cost, cntOpt; cin >> cost >> cntOpt;
    while (cntOpt--) {
      int nbOpt, costOpt; cin >> nbOpt >> costOpt;
      cost += nbOpt * costOpt;
    }

    cout << cost << "\n";
  }

  return 0;
}