안녕하세요. 오늘은 꿀벌을 분류할 거예요.
https://www.acmicpc.net/problem/9733
string 으로 while 안에 cin을 합시다. 그러면 끝까지 다 받아집니다.
그리고 각 단어에 맞게 개수를 세줍시다. 그리고 비율을 출력해주면 됩니다.
#include <iostream>
#include <string>
#define ll long long
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
string s;
ll Re = 0, Pt = 0, Cc = 0, Ea = 0, Tb = 0, Cm = 0, Ex = 0, tot = 0;
while (cin >> s)
{
if (s == "Re") Re++;
if (s == "Pt") Pt++;
if (s == "Cc") Cc++;
if (s == "Ea") Ea++;
if (s == "Tb") Tb++;
if (s == "Cm") Cm++;
if (s == "Ex") Ex++;
tot++;
}
cout << "Re " << Re << ' ';
cout.precision(2); cout << fixed;
cout << (double)(Re) / (double)(tot) << "\n";
cout.precision(0);
cout << "Pt " << Pt << ' ';
cout.precision(2); cout << fixed;
cout << (double)(Pt) / (double)(tot) << "\n";
cout.precision(0);
cout << "Cc " << Cc << ' ';
cout.precision(2); cout << fixed;
cout << (double)(Cc) / (double)(tot) << "\n";
cout.precision(0);
cout << "Ea " << Ea << ' ';
cout.precision(2); cout << fixed;
cout << (double)(Ea) / (double)(tot) << "\n";
cout.precision(0);
cout << "Tb " << Tb << ' ';
cout.precision(2); cout << fixed;
cout << (double)(Tb) / (double)(tot) << "\n";
cout.precision(0);
cout << "Cm " << Cm << ' ';
cout.precision(2); cout << fixed;
cout << (double)(Cm) / (double)(tot) << "\n";
cout.precision(0);
cout << "Ex " << Ex << ' ';
cout.precision(2); cout << fixed;
cout << (double)(Ex) / (double)(tot) << "\n";
cout.precision(0);
cout << "Total " << tot << " 1.00";
}
감사합니다.