BOJ9375

김현민·2021년 3월 9일
0

Algorithm

목록 보기
34/126
post-thumbnail

BOJ9375. 패션왕 신해빈

문제

코드

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char const *argv[])
{
    int tcase, num;
    string clothes, cate;

    cin >> tcase;

    while (tcase--)
    {
        map<string, int> m;

        cin >> num;

        while (num--)
        {

            cin >> clothes >> cate;

            if (m.find(cate) == m.end())
            {
                m.insert(pair<string, int>(cate, 1));
            }
            else
            {
                m[cate]++;
            }
        }
        int ans = 1;
        for (auto i : m)
        {
            ans *= (i.second + 1);
        }
        ans -= 1;
        cout << ans << '\n';
    }
    return 0;
}

풀이

(headgear종류 수 + 1) * (eyewear종류 수 + 1) - 1
+ 1은 그 의상을 입지 않은 경우임
- 1은 모두 입지 않을 경우
동시에 일어나는 경우이므로 곱해준다. (곱의 법칙)

Map 자료구조

  1. key, value로 pair객체 형태로 이루어짐
  2. m.insert를 하기 위해서 pair형태로 삽입해야 한다.
    m.insert(pair<string,int>(cate,1));

map.find(찾을거)에 대해서

'찾을거'를 찾게 되면 그에 해당하는 value를 리턴하고 찾지 못하면 m.end() (여기서는 0이 될것)를 리턴한다.

for(auto 변수: 배열) (c++11버전 이상)

범위기반 For문
안정성을 위해서 업그레이드 된 배열이라고 한다.
배열의 범위에 맞춰서 변수에 배열 값이 할당되는 방식
auto는 배열에 맞춰 자동으로 컴퓨터가 자료형을 판단하는 키워드

int fibonacci[]= {0,1,1,2,3,5,8,13,21}
for(int number : fibonacci){
cout<<number<<' ';

// auto로 하면 컴퓨터가 알아서 자료형을 판단함
for(auto number : fibonacci){
cout<<number<<' ';
profile
Jr. FE Dev

0개의 댓글