[백준 5361] 전투 드로이드 가격 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
전투 드로이드를 구성하는 부품 수량이 주어졌을 때, 총 가격을 계산하는 문제입니다.
- 부품의 종류는 총
5
가지이며, 각 부품의 단가는 고정되어 있습니다:
$350.34
,$230.90
,$190.55
,$125.30
,$180.90
순서로 적용됩니다.
- 각 테스트케이스마다 부품별 수량이 주어지고, 총 비용을 소수점 아래 두 자리까지 반올림하여 출력합니다.
접근법
- 고정된 단가의 배열을 준비해두고, 각 테스트케이스마다 부품 수량을 입력받습니다.
- 수량과 단가를 곱한 값을 모두 더해 총합을 계산합니다.
- 출력 시,
$
기호를 붙이고 소수점 아래2자리
까지고정 소수점 형식
으로 출력해야 합니다.
Code
[ C# ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Globalization;
class Program {
static void Main() {
double[] cost = {350.34, 230.90, 190.55, 125.30, 180.90};
int t = int.Parse(Console.ReadLine());
while (t-- > 0) {
var input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
double sum = 0;
for (int i = 0; i < 5; i++)
sum += cost[i] * input[i];
Console.WriteLine($"${sum: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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double cost[5] = {350.34, 230.90, 190.55, 125.30, 180.90};
int t; cin >> t;
while (t--) {
double sum = 0;
for (int i = 0; i < 5; i++) {
int cntP; cin >> cntP;
sum += cost[i] * cntP;
}
cout.setf(ios::fixed); cout.precision(2);
cout << "$" << sum << "\n";
}
return 0;
}