문제
https://school.programmers.co.kr/learn/courses/30/lessons/12981
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
문제 적힌 것을 그대로 체크하면 됨
단, 턴 계산을 잘해야 됨.
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
가장 먼저 탈락하는 사람의 번호
+
그 사람의 몇 번째 차례일 때 탈락하는 지
1. 앞 단어의 마지막 알파벳과 지금 단어의 첫 알파벳을 체크.
-> 다르면 탈락
2. 탈락하지 않는 경우도 존재.
*/
vector<int> solution(int n, vector<string> words) {
vector<int> answer;
vector<string> passWords; // 말한 단어 (유효한 것)
bool isT = false;
int curNum = 1; // 현재 진행하는 번호
int curTurn = 0; // 현재 몇 번째 차례인지
passWords.push_back(words[0]); // 첫 번째는 무조건 유효
for(int i=1; i< words.size(); ++i)
{
if(curNum %n ==0)
curTurn++;
curNum++;
// 말한 마지막 문자와 말하려고 하는 첫 문자가 같지 않다면
char front = words[i].front();
char end = words[i-1].back();
if(front == end)
{
if(find(passWords.begin(), passWords.end(), words[i]) == passWords.end())
{
passWords.push_back(words[i]);
}
else
{
isT = true;
break;
}
}
else
{
isT = true;
break;
}
}
curTurn++; // 마지막 턴 계산
// 번호 계산
if(curNum %n ==0)
curNum = n;
else
curNum %=n;
// 모두 올바르게 정답을 말한 경우
if(isT == false)
{
curNum = 0;
curTurn = 0;
}
// 디버깅 용
cout << curNum << " " << curTurn<< endl;
answer.push_back(curNum);
answer.push_back(curTurn);
return answer;
}
'코딩 테스트 > C++' 카테고리의 다른 글
| 프로그래머스, C++) Lv 2. 의상 (0) | 2025.10.24 |
|---|---|
| 프로그래머스, C++) Lv 3. 네트워크 (0) | 2025.10.23 |
| 프로그래머스, C++) Lv 2. 멀리뛰기 (0) | 2025.10.22 |
| 프로그래머스, C++) Lv 2. 귤 고르기 (0) | 2025.10.20 |
| 프로그래머스, C++) Lv 2. 구명 보트 (0) | 2025.10.19 |