PAT 乙级(Basic Level)kotlin版 1041-1045 &1045 C++

1041 ac

import java.io.*

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

    val ids = LongArray(1001)
    val nums = IntArray(1001)
    st.nextToken()
    val count = st.nval.toInt()
    for (i in 0 until count){
        st.nextToken()
        val id = st.nval.toLong()
        st.nextToken()
        val num = st.nval.toInt()
        st.nextToken()
        val realNum = st.nval.toInt()
        ids[num] = id
        nums[num] = realNum
    }
    st.nextToken()
    val userCount = st.nval.toInt()
    for (i in 0 until userCount){
        st.nextToken()
        val num = st.nval.toInt()

        kout.println("${ids[num]} ${nums[num]}")
    }
    kout.flush()
}
1042 ac

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

fun main(args: Array) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val s = kin.readLine().toLowerCase()
    val ch = IntArray(27)
    var length = -1
    var char = 'a'
    s.forEach {
        if (it in 'a'..'z'){
            ch[it-'a']++
            if (ch[it-'a']==length && itlength){
                length = ch[it-'a']
                char = it
            }
        }
    }
    print("$char $length")
}
1043 ac

import java.io.*

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

    st.nextToken()
    val s = st.sval
    val chars = IntArray(6) //PATest
    val res = arrayOf('P','A','T','e','s','t')
    s.forEach {
        when (it) {
            'P' -> chars[0]++
            'A' -> chars[1]++
            'T' -> chars[2]++
            'e' -> chars[3]++
            's' -> chars[4]++
            't' -> chars[5]++
        }
    }
    var count = 0
    chars.forEach {
        count+=it
    }
    while (true){
        for (i in chars.indices){
            if (chars[i]>0){
                chars[i]--
                count--
                kout.print(res[i])
            }
        }
        if (count<=0)
            break
    }
    kout.flush()
}

晚安 地球人 (Live)

1044 火星数字 ac

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

fun main(args: Array) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val kout = PrintWriter(OutputStreamWriter(System.out))
    val count = kin.readLine().toInt()
    for (i in 0 until count){
        val s = kin.readLine()
        if (s[0] in '0'..'9'){
            kout.println(earthToMars(s.toInt()))
        } else{
            kout.println(marsToEarth(s))
        }
    }
    kout.flush()

}

fun marsToEarth(s:String):Int{
    val unit = arrayOf("tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec")
    val tens = arrayOf("","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou")
    val ss = s.split(" ")
    return if (ss.size == 2){
        tens.indexOf(ss[0])*13 + unit.indexOf(ss[1])
    } else{
        if (tens.contains(ss[0]))
            tens.indexOf(ss[0])*13
        else
            unit.indexOf(ss[0])
    }
}

fun earthToMars(i :Int):String{
    val unit = arrayOf("tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec")
    val tens = arrayOf("","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou")
    return if (i < 13)
        unit[i]
    else{
        val sb = StringBuilder()
        sb.append(tens[i/13])
        if (i%13!=0)
            sb.append(" ").append(unit[i%13])
        sb.toString()
    }
}

1045 严重超时 但是思路没问题
最后有c++的版本

//1045 快速排序 除测试点1全超时

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))
    //st.nextToken()
    //val count = st.nval.toInt()
    val count = kin.readLine().toInt()
    val s = kin.readLine().split(" ")
    val sb = StringBuilder()
    var counti = 0
    val origin = IntArray(count) {
        s[it].toInt()
    }

    val left = IntArray(count)
    val right = IntArray(count)

    for (i in 0 until count) {
        val rp = count - i - 1
        if (i == 0)
            left[i] = origin[i]
        else
            left[i] = if (left[i - 1] > origin[i]) left[i - 1] else origin[i] //左边的最大值

        if (rp == count - 1)
            right[rp] = origin[rp]
        else
            right[rp] = if (right[rp + 1] < origin[rp]) right[rp + 1] else origin[rp] //右边的最小值
    }

    for (i in origin.indices) {
        if (origin[i] == left[i] && origin[i] == right[i]) {
            counti++
            sb.append(origin[i]).append(" ")
        }
    }
    kout.println(counti)
    kout.println(sb.trim())
    kout.flush()
}

vscode不好用。。。


QQ截图20201110173350.png
#include 
#include 

using namespace std;

int main()
{
    int count;
    int counti = 0;
    cin >> count;
    int origin[count], left[count], right[count], result[count];

    for (int i = 0; i < count; i++)
    {
        cin >> origin[i];
    }

    for (int i = 0; i < count; i++)
    {
        const int rp = count - i - 1;
        if (i == 0)
            left[i] = origin[i];
        else
        {
            if (left[i - 1] > origin[i])
                left[i] = left[i - 1];
            else
                left[i] = origin[i];
        }

        if (rp == count - 1)
            right[rp] = origin[rp];
        else
        {
            if (right[rp + 1] < origin[rp])
            {
                right[rp] = right[rp + 1];
            }
            else
            {
                right[rp] = origin[rp];
            }
        }
    }

    for (int i = 0; i < count; i++)
    {
        if (origin[i] == left[i] && origin[i] == right[i])
        {
            result[counti] = origin[i];
            counti++;
        }
    }
    cout << counti << endl;
    if (counti == 0)
    {
        printf("\n");
    }
    else
    {
        for (int i = 0; i < counti; i++)
        {
            cout << result[i];
            if (i != counti - 1)
            {
                printf(" ");
            }
        }
    }

    return 0;
}

你可能感兴趣的:(PAT 乙级(Basic Level)kotlin版 1041-1045 &1045 C++)