문제
각 영양소의 함량과 일일 기준량이 주어질 때, 1% 이상인 항목은 퍼센트와 함께 출력하고 미미한 항목은 별도로 모아 출력하는 문제
풀이
각 영양소에 대해 (함량 / 기준량) * 100으로 퍼센트를 계산한다. 1% 이상이면 바로 출력하고, 미만이면 별도 벡터에 저장하여 마지막에 출력한다.
코드
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
double A, R;
string U, V;
vector<string> insignificant;
while (cin >> A && A >= 0) {
cin >> U >> R;
string dummy;
getline(cin, V);
if (!V.empty() && V[0] == ' ') {
V = V.substr(1);
}
double percent = (A / R) * 100.0;
int P = (int)round(percent);
if (percent >= 1.0) {
cout << V << " " << fixed << setprecision(1) << A << " " << U << " " << P << "%\n";
} else {
insignificant.push_back(V);
}
}
cout << "Provides no significant amount of:\n";
for (const string& name : insignificant) {
cout << name << "\n";
}
return 0;
}복잡도
- 시간: O(n) (n: 영양소 개수)
- 공간: O(n)