PAT 乙级(Basic Level)kotlin版 1037-1040

//ac 1037
import java.io.BufferedReader
import java.io.InputStreamReader

//1-17 17*29
//十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可
fun main(args: Array) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val s = kin.readLine().split(" ")
    val p = s[0].split(".")
    val a = s[1].split(".")

    val cp = p[0].toInt() * 29 * 17 + p[1].toInt() * 29 + p[2].toInt()
    val ca = a[0].toInt() * 29 * 17 + a[1].toInt() * 29 + a[2].toInt()
    val remain = ca - cp
    when {
        remain > 0 -> {
            print("${remain / (29 * 17)}.${remain % (17 * 29) / 29}." +
                    "${(remain - (remain / (17 * 29)) * (17 * 29) - (remain % (17 * 29) / 29) * 29)}")
        }
        remain == 0 -> {
            print("0.0.0")
        }
        else -> {
            val r = -remain
            print("-${r / (29 * 17)}.${r % (17 * 29) / 29}." +
                    "${(r - (r / (17 * 29)) * (17 * 29) - (r % (17 * 29) / 29) * 29)}")
        }
    }
}
//1038 超时1
package pat_basic

import java.io.*
import java.lang.StringBuilder

fun main(args: Array) {
    val st = StreamTokenizer(BufferedReader(InputStreamReader(System.`in`)))

    val score = IntArray(101)
    st.nextToken()
    val count = st.nval.toInt()
    for (i in 0 until count){
        st.nextToken()
        score[st.nval.toInt()]++
    }
    st.nextToken()
    val scount = st.nval.toInt()
    val sb = StringBuilder()
    for (i in 0 until scount){
        st.nextToken()
        sb.append(score[st.nval.toInt()]).append(" ")
    }
    println(sb.trim())
}
ac 1039

import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array) {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val seller = br.readLine()
    val xiaohong = br.readLine()
    val flag = IntArray(150)
    seller.forEach {
        flag[it.toInt()]++
    }

    var count = 0
    xiaohong.forEach {
        if (flag[it.toInt()] > 0)
            flag[it.toInt()]--
        else
            count++
    }

    if (count == 0) {
        print("Yes " + (seller.length - xiaohong.length))
    } else {
        print("No $count")
    }
}

//1040 通过的代码

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.io.PrintWriter

fun main(args: Array) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val kout = PrintWriter(OutputStreamWriter(System.out))

    val s = kin.readLine()

    var patCount = 0L
    var atCount = 0L
    var tCount = 0L

    for (i in s.lastIndex downTo 0){
        if (s[i]=='P'){
            patCount+=atCount
        } else if (s[i]=='A'){
            atCount+=tCount
        } else if (s[i]=='T'){
            tCount++
        }
    }
    kout.println(patCount%1000000007)
    kout.flush()
}
//1040 超时的代码
//思路有了改起来还是很快的

import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array) {

    val kin = BufferedReader(InputStreamReader(System.`in`))
    val s = kin.readLine()
    var nextPIndex = -1
    var nextAIndex = -1
    var nextTIndex = -1

    var pCount = 0
    var aCount = 0
    var tCount = 0

    val nextLetterPosition = IntArray(s.length)//下一个同字母位置
    val nextAnotherLetterPosition = IntArray(s.length)//下一个不同字母位置
    val count = IntArray(s.length)//t个数[A用 T改]

    var patCount = 0L

    //s.indices
    for (i in s.lastIndex downTo 0){
        if (s[i]=='P'){
            pCount++
            nextLetterPosition[i] = nextPIndex
            nextAnotherLetterPosition[i] = nextAIndex
            nextPIndex = i
        } else if (s[i]=='A'){
            nextLetterPosition[i] = nextAIndex
            nextAnotherLetterPosition[i] = nextTIndex
            count[i] = tCount
            nextAIndex = i
            aCount++
        } else if (s[i]=='T'){
            tCount++
            nextLetterPosition[i] = nextTIndex
            //nextAnotherLetterPosition[i] = nextTIndex
            //count[i] = tCount
            nextTIndex = i
        }
    }

    while (nextPIndex in 0..s.lastIndex){
        nextAIndex = nextAnotherLetterPosition[nextPIndex]
        while (nextAIndex in 0..s.lastIndex){
            patCount +=count[nextAIndex]
            nextAIndex = nextLetterPosition[nextAIndex]
        }
        nextPIndex = nextLetterPosition[nextPIndex]
    }
    print(patCount%1000000007)
}

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