[백준 16673] 고려대학교에는 공식 와인이 있다 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
매년 일정한 규칙에 따라 와인을 구매할 때, C년 동안 수집한 와인의 총 개수를 구하는 문제입니다. n년 차에는 K×n + P×n² 병을 구매합니다.
접근법
1년 차부터 C년 차까지 각 연도에 구매하는 와인 수를 계산하여 모두 더합니다. 각 연도 n에서 K×n + P×n² 병을 구매하므로, 이 값을 누적하여 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
class Program {
static void Main() {
var parts = Console.ReadLine()!.Split();
var c = int.Parse(parts[0]);
var k = int.Parse(parts[1]);
var p = int.Parse(parts[2]);
var total = 0;
for (var i = 1; i <= c; i++)
total += k * i + p * i * i;
Console.WriteLine(total);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int c, k, p; cin >> c >> k >> p;
int total = 0;
for (int i = 1; i <= c; i++)
total += k * i + p * i * i;
cout << total << "\n";
return 0;
}