PAT 乙级(Basic Level)kotlin版 1012-1014

//pat 1012 没AC...?不知道哪里错了
import java.util.*

fun main(args: Array) {
    val kin = Scanner(System.`in`)
    val count = kin.nextInt()
    var tempInt:Int
    var tempString:String
    val array = intArrayOf(0,0,0,0,0)
    var flag = true
    var a4count = 0
    for (i in 0 until count){
        tempString = kin.next()
        tempInt = tempString.toInt()

        if (tempString.endsWith('0')){
            array[0] +=tempInt
        }

        else if (tempString.endsWith('1') || tempString.endsWith('6')){
            if (flag) array[1]+=tempInt
            else array[1]-=tempInt
            flag = !flag
        }

        else if (tempString.endsWith('2')||tempString.endsWith('7')) array[2]++
        else if (tempString.endsWith('3')||tempString.endsWith('8')) {
            array[3]+=tempInt
            a4count++
        }

        else if (tempString.endsWith('4')||tempString.endsWith('9')) {
            if (array[4] < tempInt)
                array[4] = tempInt
        }
    }

    for (i in 0..4){
        when {
            i == 3 -> {
                when (a4count) {
                    0 -> print("N")
                    else -> {
                        val str: String = String.format("%.1f", (array[3].toFloat() / a4count.toFloat()))
                        print(str)
                    }
                }
            }
            array[i]!=0 -> {
                print(array[i])
            }
            else -> {
                print("N")
            }
        }

        if (i!=4)
            print(" ")
    }


}
QQ截图20201005095339.png
//pat1013 有一个样例超时了...其他的都正确
//还是要用c艹

import java.util.*
import kotlin.math.sqrt

fun main(args: Array) {
    val kin = Scanner(System.`in`)
    var start = kin.nextInt()//[s,e] 从1开始
    val end = kin.nextInt()
    var result = ArrayList(end - start + 1)
    var count = 0
    var i = 2
    while (count < end){
        if (i.isPrime2()) {
            count++
            if (count in start..end)
                result.add(i)
        }
        i+=1
    }

    val sb = StringBuilder()
    count = 0
    result.forEach {
        sb.append(it)
        if (count < 9)//0..8
            sb.append(" ")
        else {//9
            sb.append("\n")
            count = -1
        }
        count++
    }

    print(sb.trim())

}

fun Int.isPrime2(): Boolean {
    if (this > 2 && this%2==0) return false

    return when {
        //equals(1) -> true
        equals(2) -> true
        equals(3) -> true
        else -> {
            val half = sqrt(this.toDouble()) + 1
            for (i in 3..half.toInt() step 2) {
                if (this % i == 0)
                    return false
            }
            return true
        }
    }
}
pat 1014 ac

import java.lang.Exception
import java.util.*

fun main(args: Array) {
    //val a = readLine()
    val kin = Scanner(System.`in`)
    val a1 = kin.next()
    val a2 = kin.next()
    val b1 = kin.next()
    val b2 = kin.next()
    var start = -1
    val days = arrayOf("MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN")

    for (i in 0..(if (a1.length < a2.length) a1.length else a2.length)) {
        if (a1[i] == a2[i] && a1[i] in 'A'..'G') {
            print("${days[a1[i] - 'A']} ")
            start = i + 1
            break
        }
    }//week

    for (i in start..(if (a1.length < a2.length) a1.length else a2.length)) {
        if (a1[i] == a2[i] && a1[i] in '0'..'9') {
            print("0${a1[i] - '0'}:")
            break
        } else if (a1[i] == a2[i] && a1[i] in 'A'..'N') {
            print("${a1[i] - 'A' +10}:")
            break
        }
    }//h

    for (i in 0..(if (b1.length > b2.length) b1.length else b2.length)) {
        if (b1[i] == b2[i] && (b1[i] in 'a'..'z' || b1[i] in 'A'..'Z')) {
            if (i < 10)
                print("0$i")
            else
                print(i)
            break
        }
    }//min
}

1015本地试了一下能过,但是oj只通过了两个用例,其他都是超时
等会写个c++的版本

你可能感兴趣的:(PAT 乙级(Basic Level)kotlin版 1012-1014)