[백준 26530] Shipping (C#, C++) - soo:bak
작성일 :
문제 링크
설명
단순한 사칙연산 문제입니다. 
입력으로 주어지는 상품들의 갯수와 가격을 적절히 계산하여, 
상품들의 총 가격을 출력합니다. 
Code
[ C# ] 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
namespace Solution {
  class Program {
    static void Main(string[] args) {
      var n = int.Parse(Console.ReadLine()!);
      for (var i = 0; i < n; i++) {
        var x = int.Parse(Console.ReadLine()!);
        var totalPrice = 0.0;
        for (var j = 0; j < x; j++) {
          var input = Console.ReadLine()?.Split(' ');
          var item = input![0];
          var quantity = int.Parse(input![1]);
          var price = double.Parse(input![2]);
          totalPrice += quantity * price;
        }
        Console.WriteLine($"${totalPrice:F2}");
      }
    }
  }
}
[ C++ ] 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n; cin >> n;
  for (int i = 0; i < n; i++) {
    int x; cin >> x;
    double totalPrice = 0.0;
    for (int j = 0; j < x; j++) {
      string item; int quantity; double price;
      cin >> item >> quantity >> price;
      totalPrice += quantity * price;
    }
    cout.setf(ios::fixed); cout.precision(2);
    cout << "$" << totalPrice << "\n";
  }
  return 0;
}