一个非常基本的锻炼帮助-----科特琳

问题描述 投票:-2回答:1

我正在尝试执行此练习

https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen

我已经编写了代码,但是结果不正确,对我来说,这一切都很好

有人可以告诉我怎么了吗?thx


    import java.util.Scanner
fun main(){
    var loop = 0
var score = Array<Int>(2){0}
val reader = Scanner(System.`in`)
var alice:String = readLine().toString()
    var bob:String = readLine().toString()
    val numerosa: List<String> =  alice.split(" ")
    val numerosb:List<String> = bob.split(" ")
    for(a in 3..3) {

        when (numerosa[loop].toInt()) {
            in numerosb[loop].toInt() + 1..100 -> score[0] += 1
            in numerosb[loop].toInt() - 1..0 -> score[1] += 1
        }
        loop += 1
    }
println("${score[0]} ${score[1]}")

}
kotlin
1个回答
0
投票

您可以这样做,您有多个不需要的变量,所以我清理了代码。

var score = Array(2) { 0 }
var alice: String = readLine() ?: ""
var bob: String = readLine() ?: ""
val aliceNumbers = alice.split(" ").map(String::toInt)
val bobNumbers = bob.split(" ").map(String::toInt)

require(aliceNumbers.size == 3 && bobNumbers.size == 3) { "There must be 3 numbers for each" }
for (a in 0..2) {
    if(aliceNumbers[a] > bobNumbers[a]) score[0] += 1
    if(aliceNumbers[a] < bobNumbers[a]) score[1] += 1
}
println("${score[0]} ${score[1]}")
© www.soinside.com 2019 - 2024. All rights reserved.