문제 설명
개미 군단이 사냥을 나가려고 합니다. 개미군단은 사냥감의 체력에 딱 맞는 병력을 데리고 나가려고 합니다. 장군개미는 5의 공격력을, 병정개미는 3의 공격력을 일개미는 1의 공격력을 가지고 있습니다. 예를 들어 체력 23의 여치를 사냥하려고 할 때, 일개미 23마리를 데리고 가도 되지만, 장군개미 네 마리와 병정개미 한 마리를 데리고 간다면 더 적은 병력으로 사냥할 수 있습니다. 사냥감의 체력 hp가 매개변수로 주어질 때, 사냥감의 체력에 딱 맞게 최소한의 병력을 구성하려면 몇 마리의 개미가 필요한지를 return하도록 solution 함수를 완성해주세요.
입출력 예
나의 풀이
class Solution {
public int solution(int hp) {
int answer = 0;
int i = 0;
while(hp > 0){
if((int)Math.floor(hp / 5) > 0){
i = (int)Math.floor(hp / 5);
answer += i;
hp -= i * 5;
} else if((int)Math.floor(hp / 3) > 0){
i = (int)Math.floor(hp / 3);
answer += i;
hp -= i * 3;
} else {
i = (int)Math.floor(hp / 1);
answer += i;
hp -= i * 1;
}
}
return answer;
}
}
참고 풀이 1 (간단)
class Solution {
public int solution(int hp) {
return hp / 5 + (hp % 5 / 3) + hp % 5 % 3;
}
}
참고 풀이 2 (나머지 이용)
class Solution {
public int solution(int hp) {
int answer = hp / 5;
hp %= 5;
answer += hp / 3;
hp %= 3;
answer += hp / 1;
return answer;
}
}
나의 풀이
function solution(hp) {
var answer = 0;
answer += Math.floor(hp / 5);
hp %= 5;
answer += Math.floor(hp / 3);
hp %= 3;
answer += Math.floor(hp / 1);
hp %= 1;
return answer;
}
참고 풀이 (간단)
function solution(hp) {
return Math.floor(hp / 5) + Math.floor((hp % 5) / 3) + (hp % 5) % 3;
}