PAT 乙级(Basic Level)kotlin版 1001-1006

https://pintia.cn/problem-sets/994805260223102976/problems/type/7 地址

//1001 https://pintia.cn/problem-sets/994805260223102976/problems/994805325918486528
import java.util.*

fun main() {
    val kin = Scanner(System.`in`)
    var step = 0;
    var i = kin.nextInt()
    while (i!=1){
        if (i%2 == 0)
            i /= 2
        else
            i = (3*i+1)/2
        step++
    }
    print(step)
}
//1002 https://pintia.cn/problem-sets/994805260223102976/problems/994805324509200384

import java.lang.StringBuilder

fun main(args: Array) {
    val s = readLine()
    val abc = arrayOf("ling","yi","er","san","si","wu","liu","qi","ba","jiu")

    val temp = s?.run {
        var result = 0
        forEach {
            result += it.toInt()
            result-=48
        }
        result
    }.toString()
    val result = temp.run {
        var result = StringBuilder()
        forEach {
            result.append(abc[it.minus('0')])
            result.append(" ")
        }
        result.deleteCharAt(result.length-1)
    }
    print(result)
}
//1003 https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192

import java.util.*

fun main(args: Array) {
    val kin = Scanner(System.`in`)
    var count = kin.nextLine().toInt()
    var ppos = -1
    var tpos = -1
    var flag = true
    while (count > 0) {
        count--
        flag = true
        val s = kin.nextLine()
        s.apply {
            val chars = toCharArray()
            for (i in 0 until length) {
                if (chars[i] == 'A') {
                    continue
                } //nothing
                if (chars[i] == 'P' && ppos >= 0) {
                    flag = false
                    return@apply
                } else if (chars[i] == 'P' && ppos < 0)
                    ppos = i

                else if (chars[i] == 'T' && tpos >= 0) {
                    flag = false
                    return@apply
                } else if (chars[i] == 'T' && tpos < 0)
                    tpos = i

                else{
                    flag = false
                    return@apply
                }
            }

            if (ppos+1 == tpos){
                flag = false
                return@apply
            }
            flag = (ppos*(tpos-ppos-1))==(length-tpos-1)
            //print(this+"   ")
        }//end apply
        if (flag)
            println("YES")
        else
            println("NO")
        flag = true
        ppos = -1
        tpos = -1
    }
}
//1004 https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448
package pat_basic

import java.util.*

fun main(args: Array) {
    val kin = Scanner(System.`in`)
    val count = kin.nextInt()
    var hname:String = ""
    var hid:String= ""
    var hscore:Int = 0

    var lname:String= ""
    var lid:String= ""
    var lscore:Int = 100

    var tname:String
    var tid:String
    var tscore:Int

    for (i in 0 until count){
        tname = kin.next()
        tid = kin.next()
        tscore = kin.nextInt()

        if (tscore > hscore){
            hname = tname
            hid = tid
            hscore = tscore
        }

        if (tscore < lscore) {
            lname = tname
            lid = tid
            lscore = tscore
        }
    }

    println("$hname $hid")
    println("$lname $lid")
}
//1005 https://pintia.cn/problem-sets/994805260223102976/problems/994805320306507776
package pat_basic

import java.util.*

fun main(args: Array) {

    val KEY = -1
    val TMP = 2

    val kin = Scanner(System.`in`)
    var temp = IntArray(101)
    var tempInt : Int

    val count = readLine()?.toInt() ?: 0
    for (i in 0 until count) {
        tempInt = kin.nextInt()
        if (temp[tempInt] != TMP)
            temp[tempInt] = KEY
        else
            continue

        while (tempInt != 1) {
            if (tempInt % 2 == 0)
                tempInt /= 2
            else
                tempInt = (tempInt * 3 + 1) / 2

            if (tempInt < 100 && temp[tempInt] != TMP)
                temp[tempInt] = TMP
            else if (tempInt < 100 && temp[tempInt] != TMP)
                break
        }
    }//end for
    val sb = StringBuilder()
    for (i in 100 downTo 0) {
        if (temp[i] == KEY)
            sb.append(i)
                .append(" ")
    }

    if (sb.isNotEmpty())
        sb.deleteCharAt(sb.length - 1)

    print(sb)
}
//1006 https://pintia.cn/problem-sets/994805260223102976/problems/994805318855278592
package pat_basic

fun main(args: Array) {
    val a = readLine()
    a?.run {
        var l = length
        var tmp:Int
        forEach {
            tmp = (it-'0').toInt()
            for (i in 1..tmp){
                if (l == 3)
                    print('B')
                if (l == 2)
                    print("S")
                if (l == 1)
                    print(i)
            }
            l--
        }
    }
}
result

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