일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- SQL Injection
- 취약점진단
- ToDoList
- 취약점
- 웹해킹
- meterpreter
- algotithm
- 써니나타스
- StringBuilder
- SQLMap
- Algorithm
- 정보시스템
- 라우터
- hackerrank
- 모의해킹
- Router
- Suninatas
- wpscan
- stock price
- todo List
- 모드 설정
- CSRF
- metasploit
- java
- todo
- study
- programmers
- leetcode
- HTML Injection
- 미터프리터
- Today
- Total
보안 / 개발 챌린저가 목표
[AlphaGo Study] [HackerRank] [JAVA] Halloween Sale 본문
문제 설명
You wish to buy video games from the famous online video game store Mist.
Usually, all games are sold at the same price, p dollars. However, they are planning to have the seasonal Halloween Sale next month in which you can buy games at a cheaper price. Specifically, the first game you buy during the sale will be sold a p dollars, but every subsequent game you buy will be sold at exactly d dollars less than the cost of the previous one you bought. This will coutinue until the cost becomes less than or equal to m dollars, after which every game you buy will cost m dollars each.
You have s dollars in your Mist wallet. How many games can you buy during the Halloween Sale?
제한 사항
- 1 ≤ m ≤ p ≤ 100
- 1 ≤ d ≤ 100
- 1 ≤ s ≤ 10⁴
- Print a single line containing a single integer denoting the maximum number of games you can buy.
입출력 예
p, d, m, s | result |
20, 3, 6, 80 | 6 |
20, 3, 6, 85 | 7 |
입출력 예 설명
#예제 1
We have p = 20, d = 3, and m = 6, the same as in the problem statement. We also have s = 80 dollars. We can buy 6 games since they cost 20 + 17 + 14 + 11 + 8 + 6 = 76 dollars. However, we cannot buy a 7 th game. Thus, the answer is 6.
#예제 2
This is the same as the previous case, except this time we have s = 85 dollars. This time, we can buy 7 games since they cost 20 + 17 + 14 + 11 + 8 + 6 + 6 = 82 dollars. However, we cannot buy an 8 th game. Thus, the answer is 7.
www.hackerrank.com/challenges/halloween-sale/problem
문제를 풀기 전 THINK
- 모든 게임은 p dollars 였으나, Halloween Sale로 인해 싸게 구입 가능하다.
- 첫 번째 게임은 p dollars, 그 다음 게임부터는 전에 구입한 가격 - d dollars.
- m dollars 될 때 까지 -d dollars를 계속 한다.
- m dollars가 되면 그 다음부터는 계속 m dollars로 구입이 가능하다.
나의 Solution
public class HalloweenSale {
int howManyGames(int p, int d, int m, int s) {
// 구매한 게임 갯수
int game_count = 0;
// 돈이 다 떨어질 때 까지 반복
while(s >= 0) {
s = s - p; // 게임 구매하고 남은 돈
if(p - d >= m) {
p = p - d;
game_count++;
} else {
p = m;
// 남은 돈이 -면 game_count를 증가시키지 않음
if(s > 0) {
game_count++;
}
}
}
return game_count;
}
}
'Development > Algorithm' 카테고리의 다른 글
[AlphaGo Study] [LeetCode] [JAVA] 104. Maximum Depth of Binary Tree (0) | 2020.09.22 |
---|---|
[AlphaGo Study] [HackerRank] [JAVA] Strange Counter (0) | 2020.09.17 |
[AlphaGo Study] [Programmers] [JAVA] 기능개발 (0) | 2020.09.10 |
[AlphaGo Study] [Programmers] [JAVA] 체육복 (0) | 2020.09.08 |
GitHub와 이클립스 준비 완료... (0) | 2020.09.07 |