PAT 乙级(Basic Level)kotlin版 1033 1035 1036

1033 旧键盘打字 最后一个测试点超时

//https://pintia.cn/problem-sets/994805260223102976/problems/994805288530460672

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

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

    val broken = kin.readLine().toCharArray()
    val string = kin.readLine().toCharArray()

    var printFlag: Boolean
    val upperCaseDisable = broken.contains('+')

    println(upperCaseDisable)

    string.forEach {
        printFlag = true
        val upper = it.toUpperCase()
        if (upper in broken)
            printFlag = false
        if (printFlag) {
            if (upperCaseDisable && it in 'A'..'Z') {
                //nothing
            } else
                kout.print(it)
        }//end if
    }//end for
    kout.flush()
}

1034 看起来很麻烦 不想写 鸽

1035 插入与归并 ac

//这道题有点麻烦,自己写了一遍插入和归并排序
//https://pintia.cn/problem-sets/994805260223102976/problems/994805286714327040

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


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

    st.nextToken()
    val count = st.nval.toInt()
    var isInsertion = true
    var index = -1
    var group = 1
    val origin = IntArray(count) {
        st.nextToken()
        st.nval.toInt()
    }
    val next = IntArray(count) {
        st.nextToken()
        st.nval.toInt()
    }

    //从0到index有序,index和index+1 无序
    for (i in 0 until next.size - 1) {
        if (next[i] > next[i + 1]) {
            index = i
            break
        }
    }
    //println("index = $index")

    if (index == -1) {
        println("Insertion Sort")
        val sb = StringBuilder()
        next.forEach {
            sb.append(it).append(" ")
        }
        print(sb.trim())
        return
    }

    for (i in index+1 until count) {
        if (origin[i] != next[i]) {
            isInsertion = false
            break
        }
    }

    if (isInsertion)
        kout.println("Insertion Sort")
    else
        kout.println("Merge Sort")


    if (isInsertion) {
        insertSort2(0, index + 1,next)
        val sb = StringBuilder()
        next.forEach {
            sb.append(it).append(" ")
        }
        kout.print(sb.trim())
    } else {//归并排序 11 12 13 14 15 16 1 2
        while (true){
            mergeSortStep(group,origin)
            group*=2
            if (origin.contentEquals(next)) {
                break
            }
        }
        val sb = StringBuilder()
        mergeSortStep(group,origin)
        origin.forEach {
            sb.append(it).append(" ")
        }
        kout.print(sb.trim())
    }
    kout.flush()
}

private fun insertSort2(start: Int, end: Int, array: IntArray) {
//    Arrays.sort(array,start,end)
    var point: Int
    if (start > end || start < 0 || end > array.size - 1)
        return
    for (posi in start + 1..end) {
        if (array[posi - 1] > array[posi]) {//顺序错了 [posi-1][posi]
            point = posi
            while (point > start) {
                if (array[point - 1] > array[point]) {
                    array[point] += array[point - 1]
                    array[point - 1] = array[point] - array[point - 1]
                    array[point] = array[point] - array[point - 1]
                    point--
                } else {
                    break
                }
            }
        }
    }

}

private fun mergeSortStep(group: Int, array: IntArray) {
    if (group < 0 || group > array.size)
        return

    var start = 0
    var end = group - 1
    while (start < array.size) {
        if (end > array.size - 1)
            end = array.size - 1
        insertSort2(start, end, array)
        start += group
        end += group
    }
}

1036 跟卢锡安一起编程
ac

//https://pintia.cn/problem-sets/994805260223102976/problems/994805285812551680

import java.io.*

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

    val s = kin.readLine().split(" ")
    val count = s[0].toInt()
    //st.nextToken()
    val signal = s[1]

    for (i in 0 until count)
        kout.print(signal)
    kout.println()

    for (i in 2 until (count+1).shr(1)){
        kout.print(signal)
        for (j in 1 until count-1)
            kout.print(' ')
        kout.println(signal)
    }
    for (i in 0 until count)
        kout.print(signal)
    kout.flush()
}

你可能感兴趣的:(PAT 乙级(Basic Level)kotlin版 1033 1035 1036)