돌아서면 까먹어서

topcoder - 즐거운 파티 Interesting Party 18.11.23 본문

ALGORITHM

topcoder - 즐거운 파티 Interesting Party 18.11.23

양갱맨 2018. 11. 23. 17:47

<즐거운 파티>


각각의 친구들은 2가지의 흥미를 가지고 있다.

이 흥미 주제가 맞아야지만 즐거운 파티를 보낼 수 있다.

초대할 수 있는 최대 친구는 몇 명일까?

문자열 배열 first/second가 있고

i번째 친구의 흥미 주제는 first[i], second[i]이고 이 둘은 같을 수 없다.


<소스코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <algorithm>
#include <time.h>
using namespace std;
int people = 0;
int happyParty(string * first, string * second) {
    int friendNum = 0;
    for (int i = 0; i < people; ++i) {
        int localnum = 0;
        for (int j = 0; j < people; ++j) {
            if (i == j) continue;
            if (first[i] == second[j] || first[i] == first[j] || second[i] == second[j]) {
                ++localnum;
            }
        }
        friendNum = max(friendNum, localnum)+1;
    }
    return friendNum;
}
void main() {
    cout << "사람 수 입력 : ";
    cin >> people;
 
    string * first = new string[people];
    string * second = new string[people];
 
    for (int i = 0; i < people; ++i) {
        string hobby;
        cout << i << "번째 사람의 취미 입력(엔터로 구분)"<<endl;
        cin >> first[i];
        cin >> second[i];
    }
    cout << happyParty(first, second)<<"명"<<endl;
}
cs



* 풀면서 직면했던 문제

1. cin으로 입력을 받은 문자열을 문자열에 넣으려고 했더니 오류가 났었다.

   찾아보니 cin.getline()을 통해서 받는 방법이 있었는데

   다시 코드를 실행시켜보니 잘 실행됨;;

   c++이 익숙하지 않기 때문에 좀 더 살펴보고 각 메소드에 대한 공부를 해야겠다.(새로 포스팅)


2. for문 사용안하기

   for문을 사용안하고 해보자니 구조가 떠오르지 않아서 그냥 for문으로 작성..

   책에서는 map을 사용한다는데 문제만 보고 풀었기 때문에

   추가적으로 map 부분 구현해보기. (다음주)

Comments