PAT 乙级(Basic Level)kotlin版 1027-1030

//1027ac 写的不是很好

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

fun main(args: Array) {
    val kout = PrintWriter(OutputStreamWriter(System.out))
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val s = kin.readLine().split(" ")
    val count = s[0].toInt()
    val signal = s[1][0]
    var out = 3
    var total = 1

    if (count == 1) {
        kout.print("$signal\n0")
        kout.flush()
        return
    }

    while (total < count) {
        total += 2 * out
        out += 2
    }

    out -= 2 //最外符号数
    total -= 2 * out
    out -= 2 //最外符号数


    for (i in out downTo 3 step 2) {
        val spc = (out - i) shr 1
        //println("空格数 $spc")

        for (a in 0 until spc)
            kout.print(' ')
        for (b in 0 until i)
            kout.print(signal)
        kout.println()
    }

    for (a in 0 until out.shr(1))
        kout.print(' ')
    kout.println(signal)

    for (i in 3..out step 2) {
        val spc = (out - i) shr 1
        //println("空格数 $spc")

        for (a in 0 until spc)
            kout.print(' ')
        for (b in 0 until i)
            kout.print(signal)
        kout.println()
    }
    kout.print(count - total)
    kout.flush()
}
//1028
//5超时

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 count = kin.readLine().toInt()
    var maxps = Person("", 0, 0, 0)
    var minps = Person("", 9999, 9, 9)
    var validCount = 0
    for (i in 0 until count) {
        val s = kin.readLine().split(" ")
        val date = s[1].split("/")
        val ps = Person(s[0], date[0].toInt(), date[1].toInt(), date[2].toInt())
        if (ps.isValid()) {
            validCount++
            if (ps > maxps)
                maxps = ps
            if (ps < minps)
                minps = ps
        }
    }

    kout.print("$validCount${if (minps.isValid()) " ${minps.name}" else ""}${if (maxps.isValid()) " ${maxps.name}" else ""}")
    kout.flush()
}

class Person(val name: String, val year: Int, val month: Int, val day: Int) : Comparable {

    fun isValid(): Boolean {
        if (year < 1814)
            return false
        if (year > 2014)
            return false
        if (year == 1814 && month <= 9 && day <= 5)
            return false
        if (year == 2014 && month >= 9 && day >= 7)
            return false

        return true
    }

    override fun compareTo(other: Person): Int {
        if (year > other.year)
            return 1
        else if (year < other.year)
            return -1
        else {
            if (month > other.month)
                return 1
            else if (month < other.month)
                return -1
            else {
                if (day > other.day)
                    return 1
                else if (day < other.day)
                    return -1
            }
        }
        return 0
    }

    override fun toString(): String {
        return "Person(name='$name', year=$year, month=$month, day=$day)\n"
    }


}
//1029 ac
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.io.PrintWriter

fun main(args: Array) {
    val kout = PrintWriter(OutputStreamWriter(System.out))
    val kin = BufferedReader(InputStreamReader(System.`in`))
//    print("${'0'.toInt()} ${'_'.toInt()} ${'a'.toInt()} ${'A'.toInt()} ${"as"} ")
    val list = BooleanArray('z'-'0')
    val p = kin.readLine().toUpperCase().toCharArray()//原
    val t = kin.readLine().toUpperCase().toCharArray()//输入

    t.forEach {
        list[it-'0'] = true
    }

    p.forEach {
        if (!list[it-'0']){
            list[it-'0'] = true
            print(it)
        }
    }
}

//一开始是这样写的,但是不知道为什么用例5没过
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.io.PrintWriter

fun main(args: Array) {
    val kout = PrintWriter(OutputStreamWriter(System.out))
    val kin = BufferedReader(InputStreamReader(System.`in`))
//    print("${'0'.toInt()} ${'_'.toInt()} ${'a'.toInt()} ${'A'.toInt()} ${"as"} ")
    val list = BooleanArray('z'-'0')
    val p = kin.readLine().toUpperCase().toCharArray()//原
    val t = kin.readLine().toUpperCase().toCharArray()//输入
    println(p)
    println(t)
    var pos = 0
    t.forEach {
        while (it != p[pos]){
            if (!list[p[pos]-'0']) {
                list[p[pos] - '0'] = true
                kout.print(p[pos])
            }
            pos++
        }// end while
        pos++
    }
    kout.flush()
}

1030 例4超时
import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array) {
    val br = BufferedReader(InputStreamReader(System.`in`))
    var s = br.readLine().split(" ")
    val count = s[0].toInt()
    val p = s[1].toInt()

    var max = -1
    s = br.readLine().split(" ")
    val list = DoubleArray(count) { s[it].toDouble() }
    list.sort()

//注释掉的代码和下面的代码意思完全相同,但是有两个用例会错。。。?
//    for (i in list.indices) {
//        val mp = list[i] * p
//        if (mp > list.last())
//            break
//        for (j in list.size - 1 downTo i) { //list[j] = 最大值
//            if (list[j] <= mp) {
//                if ((j-i+1) > max)
//                    max = (j-i+1)
//                break
//            }
//        }
//    }

    for (i in list.indices) {
        val min = list[i] / p //list[i]最大
        for (j in 0..i) {
            if (list[j] >= min) {
                if ((i-j+1) > max)
                    max = (i-j+1)
                break
            }
        }
    }

    println(max)
}

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