Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Načítaj vstupy a vyparsuj z nich dátumy.
Urob rozdiely medzi zadaným rokom a očakávaným rokom vrátenia. Podobne pre mesiac a deň.
Porovnaj hodnoty a vypočítaj poplatok:
– ak je rozdiel rokov väčší ako 0
, poplatok je 10000
– ak je rozdiel rokov rovný 0
a rozdiel mesiacov väčší ako 0
, potom poplatok je 45 * rodiel_mesiacov
– ak je rozdiel rokov rovný 0
a rozdiel mesiacov rovný 0
a rozdiel medzi dňami väčší ako 0
, poplatok bude 15 * rozdiel_dni
– inak je poplatok 0
.
Vytvoril som riešenie v týchto programovacích jazykoch:
Všetky riešenia sú dostupné aj na mojom GitHub profile.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
object LibraryFine extends App { val consoleReader = io.Source.stdin.bufferedReader() val actualDate = consoleReader.readLine().split(" ").map(_.toInt) val expectedDate = consoleReader.readLine().split(" ").map(_.toInt) val yearDiff = actualDate(2) - expectedDate(2) val monthDiff = actualDate(1) - expectedDate(1) val dayDiff = actualDate(0) - expectedDate(0) def fee(year: Int, month: Int, day: Int): Int = { if(year > 0) return 10000 if(year == 0 && month > 0) return month * 500 if(year == 0 && month == 0 && day > 0) return day * 15 return 0 } println(fee(yearDiff, monthDiff, dayDiff)) } |
Java
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 |
import java.util.Scanner; public class LibraryFine { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int actualDay = stdin.nextInt(); int actualMonth = stdin.nextInt(); int actualYear = stdin.nextInt(); int expectedDay = stdin.nextInt(); int expectedMonth = stdin.nextInt(); int expectedYear = stdin.nextInt(); int yearDiff = actualYear - expectedYear; int monthDiff = actualMonth - expectedMonth; int dayDiff = actualDay - expectedDay; System.out.println(fee(yearDiff, monthDiff, dayDiff)); } private static int fee(int year, int month, int day) { if(year > 0) return 10000; if(year == 0 && month > 0) return month * 500; if(year == 0 && month == 0 && day > 0) return day * 15; return 0; } } |
JavaScript
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 |
function processData(input) { var lines = input.split("\n"); var actualDate = lines[0].split(" "); var expectedDate = lines[1].split(" "); var yearDiff = parseInt(actualDate[2]) - parseInt(expectedDate[2]); var monthDiff = parseInt(actualDate[1]) - parseInt(expectedDate[1]); var dayDiff = parseInt(actualDate[0]) - parseInt(expectedDate[0]); console.log(fee(yearDiff, monthDiff, dayDiff)); } function fee(year, month, day) { if(year > 0) return 10000; if(year == 0 && month > 0) return month * 500; if(year == 0 && month == 0 && day > 0) return day * 15; return 0; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
(actual_day, actual_month, actual_year) = gets.to_s.split(" ").map { |s| s.to_i } (expected_day, expected_month, expected_year) = gets.to_s.split(" ").map { |s| s.to_i } year_diff = actual_year - expected_year month_diff = actual_month - expected_month day_diff = actual_day - expected_day def fee(year, month, day) if year > 0 return 10000 end if year == 0 && month > 0 return month * 500 end if year == 0 && month == 0 && day > 0 return day * 15 end return 0 end puts fee(year_diff, month_diff, day_diff) |