Problem
Lily likes to play games with integers. She has created a new game where she determines the difference between a number and its reverse. For instance, given the number , its reverse is . Their difference is . The number reversed is , and their difference is .
She decides to apply her game to decision making. She will look at a numbered range of days and will only go to a movie on a beautiful day.
Given a range of numbered days, and a number , determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where is evenly divisible by . If a day's value is a beautiful number, it is a beautiful day. Print the number of beautiful days in the range.
Function Description
Complete the beautifulDays function in the editor below. It must return the number of beautiful days in the range.
beautifulDays has the following parameter(s):
- i: the starting day number
- j: the ending day number
- k: the divisor
Input Format
A single line of three space-separated integers describing the respective values of , , and .
Constraints
Output Format
Print the number of beautiful days in the inclusive range between and .
Sample Input
20 23 6
Sample Output
2
Explanation
Lily may go to the movies on days , , , and . We perform the following calculations to determine which days are beautiful:
- Day is beautiful because the following evaluates to a whole number:
- Day is not beautiful because the following doesn't evaluate to a whole number:
- Day is beautiful because the following evaluates to a whole number:
- Day is not beautiful because the following doesn't evaluate to a whole number:
Only two days, and , in this interval are beautiful. Thus, we print as our answer.
How I solved the problem
# 시작날 부터 마지막날까지 while문으로 돌림!
# 주어진 값에 StringBuffer의 reverse를 사용하여 reverse값을 구한 다음 Math.abs를 사용하여 주어진 값과 reverse된 값의 차이를 절대값으로 구한 다음 나누고 싶은 갚을 나누고 나머지가 0일 경우만 카운트 해줌!!!
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the beautifulDays function below. static int beautifulDays(int i, int j, int k) { int beautifulDaysCount = 0; while (i <= j) { StringBuilder buf = new StringBuilder(String.valueOf(i)); buf = buf.reverse(); int reverse = Integer.valueOf(buf.toString()); if ((Math.abs(reverse - i)) % k == 0) { beautifulDaysCount++; } i++; } return beautifulDaysCount; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] ijk = scanner.nextLine().split(" "); int i = Integer.parseInt(ijk[0]); int j = Integer.parseInt(ijk[1]); int k = Integer.parseInt(ijk[2]); int result = beautifulDays(i, j, k); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.03.07] Save the Prisoner! (0) | 2019.03.07 |
---|---|
[2019.03.06] Viral Advertisement (0) | 2019.03.06 |
[2019.03.04] Angry Professor (0) | 2019.03.04 |
[2019.02.28] Utopian Tree (0) | 2019.02.28 |
[2019.02.27] Designer PDF Viewer (0) | 2019.02.27 |
댓글