본문 바로가기

Algorithms/30 Days of Code

Day 26: Nested Logic

Task 
Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows:

  1. If the book is returned on or before the expected return date, no fine will be charged (i.e.: .
  2. If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, .
  3. If the book is returned after the expected return month but still within the same calendar year as the expected return date, the .
  4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of .

Input Format

The first line contains  space-separated integers denoting the respective , and  on which the book was actually returned. 
The second line contains  space-separated integers denoting the respective , and  on which the book was expected to be returned (due date).

Constraints

Output Format

Print a single integer denoting the library fine for the book received as input.

Sample Input

9 6 2015
6 6 2015

Sample Output

45

Explanation

Given the following return dates: 
Actual:  
Expected: 

Because , we know it is less than a year late. 
Because , we know it's less than a month late. 
Because , we know that it was returned late (but still within the same month and year).

Per the library's fee structure, we know that our fine will be . We then print the result of  as our output.


풀이


다른 사람 discussion을 확인해보니 day, month, year 각각 nextInt로 받아와도 된다.

각각 int형으로 받아와서 비교해주는게 좀 더 보기에는 편할 거 같다.

package Day27;

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
String realDate = sc.nextLine();
String dueDate = sc.nextLine();
sc.close();
String[] realDateArray = realDate.split("");
String[] dueDateArray = dueDate.split("");
String fine = checkFine(realDateArray, dueDateArray);
System.out.println(fine);
}

private static String checkFine(String[] realDateArray, String[] dueDateArray) {
int mDiffer = 0;
int dDiffer = 0;
if (Integer.parseInt(realDateArray[2]) > Integer.parseInt(dueDateArray[2])) return "10000";
else if (Integer.parseInt(realDateArray[2]) < Integer.parseInt(dueDateArray[2])) return "0";
if (Integer.parseInt(realDateArray[1]) > Integer.parseInt(dueDateArray[1])) {
mDiffer += Integer.parseInt(realDateArray[1]) - Integer.parseInt(dueDateArray[1]);
mDiffer *= 500;
} else if (Integer.parseInt(realDateArray[0]) > Integer.parseInt(dueDateArray[0])) {
dDiffer += Integer.parseInt(realDateArray[0]) - Integer.parseInt(dueDateArray[0]);
dDiffer *= 15;
}
return String.valueOf(mDiffer + dDiffer);
}
}


'Algorithms > 30 Days of Code' 카테고리의 다른 글

Day 28: RegEx, Patterns, and Intro to Databases  (0) 2018.03.12
Day 27: Testing  (0) 2018.03.12
Day 25: Running Time and Complexity  (0) 2018.03.09
Day 24: More Linked Lists  (0) 2018.03.09
Day 23: BST Level-Order Traversal  (0) 2018.03.08