본문 바로가기
C++

[프로그래머스] 개미 군단

by 띰쥬 2025. 9. 5.
728x90
반응형
SMALL

#include <string>
#include <vector>

using namespace std;

int solution(int hp) {
    // 장군개미 5
    // 병정개미 3
    // 일개미 1
    vector<int> hps = {5,3,1};
    int jang = hp/hps[0];
    int byeung = (hp-jang*hps[0])/hps[1];
    int il = (hp-jang*hps[0]-byeung*hps[1])/hps[2];;
    
    
    return jang + byeung + il;
}

 

 

 

 

* 간단하게 다음과 같이 변경 가능

#include <string>
#include <vector>

using namespace std;

int solution(int hp) {
    // 장군개미 5
    // 병정개미 3
    // 일개미 1
     int answer = 0;
    for(int h=5; h>0; h-=2){
        answer += hp/h;
        hp %= h;
    }
    return answer;
}

 

 

728x90
반응형
LIST

댓글