C++:: 프로그래머스 < 푸드 파이트 대회 >

jahlee·2023년 4월 24일
0

프로그래머스_Lv.1

목록 보기
24/75
post-thumbnail

간단하게 2개 이상이때만 해당 음식을 세팅하면 된다.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

string solution(vector<int> food)
{
    string answer = "";
    for(int i=1;i<food.size();i++)
    {
         while(food[i]>1)// 1개보다 많으면
         {
             answer.push_back(i+'0');// 해당 음식 세팅
             food[i] -= 2;// 개수 차감
         }
    }
    string r_answer = answer;
    reverse(r_answer.begin(), r_answer.end());//세팅한 음식 뒤집어준 문자열
    return answer + "0" + r_answer;//더해서 리턴
}

0개의 댓글