0
[SOLVED] Parking Fee(Kotlin) - I need help anyone.
Case 1 and 2 are successful but I don't know what is wrong. Please help. Here is my attempt:- https://code.sololearn.com/c0spe4fMPNll/?ref=app
30 odpowiedzi
+ 3
There are a number of problems with your logic.
48 hrs returns 27 instead of 30
4 hrs returns 5 instead of 4.
Read through the description again to fully understand the requirements.
Each day (24hr) charge 15 + .5 for each extra hr, so 50 hrs = 31
if not a full day (24hr), then charge 1 for the first 5 hrs & .5 for each hour after.
8 hrs = 6.5
4 hrs = 4
+ 24
Here my code for that problem :
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if(hours <= 5){
var result: Double = hours * 1.0
total = result
} else if (hours > 5 && hours < 24){
var result: Double = (hours - 5.0) * 0.5
total = 5.0 + result
} else {
var result: Double = (15.0 * Math.floor(hours /24.0)) + ((hours% 24.0)*0.5)
total = result
}
println(total)
}
+ 12
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
var day = hours /24
if(hours > 24){
total = 15*day + (hours%24)*0.5
}else if(hours > 5 && hours < 24){
total = 5 + (hours - 5)*0.5
}else if(hours > 0 && hours < 5) {
total = hours * 1.0
} else{
total =1.0
}
println(total)
}
+ 3
This is mine. I hope this can help you
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
var day : Int = hours/24
var addhours : Int = hours%24
total = when {
hours >5 && hours <24 -> 5.0+(hours-5)*0.5
hours >= 24 -> (addhours*0.5) + (day*15)
else -> hours*1.0
}
println(total)
}
+ 3
Just want to share my solution:
fun main(args: Array<String>) {
var hours = readLine()!!.toDouble()
var total: Double = 0.0
if(hours <= 5) {
total = hours *1.0
}
else if (hours > 5 && hours < 24) {
total = 5.0 + (hours - 5) * 0.5
}
else {
total = Math.floor(hours/24) * 15.0 + (hours%24) * 0.5
}
println(total)
}
+ 2
Here is the most elegant solution I can figure out:
//Created by Jacob Lynn 12/6/2021
fun main(args: Array<String>) {
var hours = 48
//We do not need to initialize total as it will be initialized the first time it is utilized, however since I am incrementing rem I need to initialize it at the beginning
var total: Double
var rem: Double = 0.0
/*The problem can be focused on a set of conditions that if one is satisfied the result can be found.*/
//First we need to find how periods of 24 hours the car was parked, then with the remaining hours multiple by 50 cents.
if (hours >= 24){
rem = Math.floor(hours.toDouble()/24) * 15
hours = hours%24
total = rem + (hours*0.5)
println("End of 24 IF statement subtotal:$rem hours:$hours")
}
//Very straight forward, if not 24 check if more than 5. Charging a block amount for the first 5 then .5 for any remaining hours
else if (hours >= 5){
rem+= 5
hours-= 5
total = rem + (hours*0.5)
println("End of 5 IF statement subtotal:$rem hours:$hours total:$total")
}
else {
total = rem + (hours*1)
}
//The println can be a powerful debugging tool
println("subtotal:$rem hours:$hours total:$total")
}
Actual Code Below
----------------------------------------------------------------
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double
var rem: Double = 0.0
if (hours >= 24){
rem = Math.floor(hours.toDouble()/24) * 15
hours = hours%24
total = rem + (hours*0.5)
}
else if (hours >= 5){
rem+= 5
hours-= 5
total = rem + (hours*0.5)
}
else {
total = rem + (hours*1)
}
println(total)
}
+ 1
if hrs > 24
Use hrs / 24 to get your days
Use hrs %24 to get your residual
else hrs < 24
if hrs > 5
(hrs -5)* 0.5 +5
else
hrs *1
+ 1
Here is my code!
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if (hours <= 5 ) {
total = hours.toDouble()
} else if(hours in 6..23) {
total = ((hours-5)* 0.5 + 5).toDouble()
}else {
total = (((hours/24) * 15) + ((hours%24)*0.5)).toDouble()
}
println(total)
}
0
friends Can Anyone Please post answers For Water consumption kotlin
project
0
fun main() {
val hours = readLine()!!.toInt()
var total: Double = 0.0
val day = hours /24
if(hours > 24){
total = 15*day + (hours%24)*0.5
}else if(hours in 6..23){
total = 5 + (hours - 5)*0.5
}else {
if((hours > 0) && (hours < 5)) {
total = hours * 1.0
} else{
total =1.0
}
}
println(total)
}
0
fun main(args: Array<String>) {
var years = readLine()!!.toInt()
val days = 365
var litres = 15
var result = years * days * litres
println(result)
}
Here is my water consumption code, It basically calculates the number of litres per day in a given number of years
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if(hours in 1..5 || hours in 6..23 || hours == 24){
when(hours){
in 1..5 -> total = hours * 1.0
in 6..23 -> total = ((hours-5)*0.5)+5.0
24 -> total = 15.0
}
}else if(hours%24 == 0){
total = 15.0 + hours%24 * 15.0
}else if (hours>24){
total = (hours/24*15)+(hours%24*0.5)
}
println(total)
}
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
var days = hours / 24
total = when {
hours >= 24 -> (15.0 * days) + 0.5 * (hours % 24)
hours <= 5 -> 1.0 * hours
else -> 5.0 + 0.5 * (hours - 5.0)
}
println(total)
}
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if (hours < 24.0) {
total = 5.0 + (hours - 5)*0.5
} else {
total = 15.0 * (hours/24) + (hours%24)*0.5
}
println(total)
}
I have no idea why it does not work for case 4
0
Oh. I did not check how it works for less than 5 🤦🏽♂️
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if(hours <= 5){
total = hours * 1.0
} else if (hours > 5 && hours < 24){
total = (hours - 5.0) * 0.5 + 5.0
} else {
total = (15.0 * Math.floor(hours /24.0)) + ((hours% 24.0)*0.5)
}
println(total)
}
0
This is my version (the key for this one is really the modulo operator to ensure the full day fees are properly being calculated):
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
var total1: Double = 0.0
var total2: Double = 0.0
var total3: Double = 0.0
var total4: Double = 0.0
var days = hours / 24
var x = 0
if (hours in 1..5) {
total1 = hours.toDouble()
println(total1)
} else if (hours <= 23) {
total2 = ((hours - 5) * 0.5.toDouble())
println(total2 + 5)
} else if (hours > 23) {
x = hours % 24
total3 = (15 * days) + (0.5 * x)
println(total3)
}
}
0
fun main() {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if(hours>23){
var days:Int = hours/24 //for the integer part
var extraHours = hours-(24*days)
total = (days*15)+(extraHours*0.5)
}else if(hours>4 && hours<24){
var extraHours = hours-5
total = 5+(extraHours*0.5)
}else{
total = hours.toDouble()
}
println(total)
}
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if (hours<=5)
{total = 1.0*hours}
else if (hours>5 && hours<24)
{total = 5 + 0.5*(hours-5)}
else if (hours>24 && hours/24!=0)
{total = 15.0*(hours/24) + 0.5*(hours-(24*(hours/24)))}
println(total)
}
Hmm i used math to solve , anyway it's take me 3 hours to think a way how to calculate this
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
var day = hours /24
if(hours > 24){
total = 15*day + (hours%24)*0.5
}else if(hours > 5 && hours < 24){
total = 5 + (hours - 5)*0.5
}else if(hours > 0 && hours < 5) {
total = hours * 1.0
} else{
total =1.0
}
println(total)
}
Try this🙂