PAT 乙级(Basic Level)kotlin版 1046-

2020-11-17  本文已影响0人  qmr777
1046 ac

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

fun main(args: Array<String>) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val count = kin.readLine().toInt()

    var aw = false
    var bw = false
    var awc = 0
    var bwc = 0

    for (i in 0 until count) {
        val ss = kin.readLine().split(" ").map {
            it.toInt()
        }
        aw = (ss[0] + ss[2] == ss[1])
        bw = (ss[0] + ss[2] == ss[3])

        if (aw == bw){
            //nothing
        }
        else if (aw)
            awc++
        else if (bw)
            bwc++
    }
    print("$bwc $awc")
}
1047 ac

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

fun main(args: Array<String>) {
    val st = StreamTokenizer(BufferedReader(InputStreamReader(System.`in`)))
    st.nextToken()
    val count = st.nval.toInt()
    val team = IntArray(1001)
    var maxgroup = 0

    for (i in 0 until count) {
        st.nextToken()
        val group = st.nval.toInt()
        st.nextToken()
        st.nextToken()
        val score = st.nval.toInt()
        team[group] += score
        if (team[maxgroup] < team[group]) {
            maxgroup = group
        }
    }
    print("$maxgroup ${team[maxgroup]}")
}

1048 ac

import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.math.abs

fun zero(len: Int): StringBuilder {
    val sb = StringBuilder()
    for (i in 0 until len) {
        sb.append('0')
    }
    return sb
}

fun main(args: Array<String>) {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val str = br.readLine().split(" ")
    val table = "0123456789JQK"
    var a = str[0].toCharArray()
    var b = str[1].toCharArray()
    val len = abs(str[1].length - str[0].length)
    if (a.size < b.size) {
        a = zero(len).append(str[0]).toString().toCharArray()
    } else if (a.size > b.size) {
        b = zero(len).append(str[1]).toString().toCharArray()
    }
    var PF = -1
    if (1 == b.size and 1) {
        PF = 1 // 奇数
    }
    for (i in b.indices) {
        var x = (b[i].toInt() - 48 + PF * (a[i].toInt() - 48)) % 13 // 位运算
        if (x < 0) {
            x += 10
        }
        print(table[x])
        PF = -PF
    }
}

1049 测试点2没通过 不知道为什么

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

fun main(args: Array<String>) {
    val st = StreamTokenizer(BufferedReader(InputStreamReader(System.`in`)))
    st.nextToken()
    val count = st.nval.toInt()
    val array = DoubleArray(count)
    //val start = IntArray(count)
    var result = 0.0
    for (i in 0 until count){
        st.nextToken()
        array[i] = st.nval
    }
    for (i in 0 until count){
        //start[i] = (count-i)*(i+1)
        result += array[i]*(count-i)*(i+1)
    }

    System.out.printf("%.2f",result)
}
1050 最后两个测试点超时

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.io.PrintWriter
import kotlin.math.sqrt

fun main(args: Array<String>) {
    val kout = PrintWriter(OutputStreamWriter(System.out))
    val br = BufferedReader(InputStreamReader(System.`in`))
    val count = br.readLine().toInt()
    val ss = br.readLine().split(" ")
    val num = IntArray(count) {
        ss[it].toInt()
    }
    num.sortDescending()
    var n = sqrt(count.toDouble()).toInt()
    while (count % n != 0) {
        --n
    }
    val m = count / n
    val matrix = Array(m) { IntArray(n) }
    var direction = 0
    var x = 0
    var y = 0
    for (i in 0 until count) {
        matrix[x][y] = num[i]
        when (direction) {
            0 -> if (y + 1 < n && 0 == matrix[x][y + 1]) {
                ++y
            } else {
                ++x
                direction = 1
            }
            1 -> if (x + 1 < m && 0 == matrix[x + 1][y]) {
                ++x
            } else {
                --y
                direction = 2
            }
            2 -> if (y - 1 >= 0 && 0 == matrix[x][y - 1]) {
                --y
            } else {
                --x
                direction = 3
            }
            else -> if (x - 1 >= 0 && 0 == matrix[x - 1][y]) {
                --x
            } else {
                ++y
                direction = 0
            }
        }
    }
    matrix.forEach {
        for (i in it.indices){
            if (i!=it.size-1)
                kout.print("${it[i]} ")
            else
                kout.println(it[i])
        }
    }
    kout.flush()
}


上一篇下一篇

猜你喜欢

热点阅读