美文网首页
PAT 乙级(Basic Level)kotlin版 1016-

PAT 乙级(Basic Level)kotlin版 1016-

作者: qmr777 | 来源:发表于2020-10-06 18:02 被阅读0次

    每天两道 提神醒脑

    //https://pintia.cn/problem-sets/994805260223102976/problems/994805306310115328
    //pat 1016
    
    import java.io.BufferedReader
    import java.io.InputStreamReader
    import kotlin.math.pow
    
    
    fun main(args: Array<String>) {
        //val kin = Scanner(System.`in`) 2个超时
        val kin = BufferedReader(InputStreamReader(System.`in`)) //ac
        //readLine()
        //val s = readLine()!!.split(' ') 全超时
        val s = kin.readLine().split(" ")
        val a = s[0].toCharArray()
        val da = s[1].toInt()
        val b = s[2].toCharArray()
        val db = s[3].toInt()
    
    //    print(result(a, da.toString().toCharArray(), da))
        print(result(a, da.toString().toCharArray(), da) + result(b, db.toString().toCharArray(), db))
    
    }
    
    fun result(p: CharArray, t: CharArray, i: Int): Int {
        var x = p.count { it == t[0] }
        var result = 0;
        while (x > 0) {
            x--
            result += i * (10.0.pow(x.toDouble())).toInt()
        }
    
        return result
    }
    
    //pat 1017
    //这道题用kotlin会超时,用BigInteger会超时用java也会超时
    //java版 能通过前3个用例
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
            String[] ss = null;
            int ti;
            int tmp = 0;
            boolean isFirst = true;
            try {
                ss = sc.readLine().split(" ");
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            if (ss != null) {
                final char[] s1 = ss[0].toCharArray();
                final int i2 = Integer.parseInt(ss[1]);
                if (s1.length < 10) {
                    int i = Integer.parseInt(String.copyValueOf(s1));
                    System.out.print(i / i2 + " " + i % i2);
                } else {
    
                    for (char c : s1) {
                        ti = c - '0';
                        tmp = tmp * 10 + ti;
                        if (tmp < i2 && isFirst) {
                            //nothing
                        } else if (tmp < i2) {
                            System.out.print(0);
                        } else {
                            ti = tmp / i2;
                            System.out.print(ti);
                            tmp -= ti*i2;
                        }
                        isFirst = false;
                    }
    
                    System.out.print(" " + tmp);
                    //17636684150141093474 3
                    //17636684150141093474 3
                }
            }
        }
    }
    
    
    
    //kt版 全超时 但是答案是对的
    package pat_basic
    
    import java.io.BufferedReader
    import java.io.InputStreamReader
    
    fun main(args: Array<String>) {
    
        val kin = BufferedReader(InputStreamReader(System.`in`))
        val ss = kin.readLine().split(' ')
        val b1 = ss[0].toCharArray() //除数
        val b2 = ss[1].toInt() //被除
        var ti = 0
        var tmp = 0
        var isfirst = true
    
        if (b1.size < 10){
            val i = ss[0].toInt()
            print("${i/b2} ${i%b2}")
        } else {
            b1.forEach {
                ti = it - '0'
                tmp = tmp * 10 + ti
                if (tmp < b2 && !isfirst) {
                    print(0)
                } else if (tmp < b2 && isfirst) {
                    //nothing
                } else {
                    ti = tmp / b2
                    print(ti)
                    tmp %= b2
                }
                isfirst = false
            }
            print(" $tmp")
        }
    }
    
    //最后一个用例超时
    //1018
    import java.util.*
    
    fun main(args: Array<String>) {
        val kin = Scanner(System.`in`)
        var total = kin.nextInt()
        var a:String
        var b:String
        val wl = intArrayOf(0,0,0)//a b
        val wina = intArrayOf(0,0,0)//cjb
        val winb = intArrayOf(0,0,0)
        while (total-- >0){
            a = kin.next()
            b = kin.next()
    
            if (a == b)
                wl[1]++
            else if (a == "J" && b == "C"){
                wl[2]++
                winb[0]++
            } else if(a == "J" && b == "B"){
                wl[0]++
                wina[1]++
            } else if (a == "C" && b == "J"){
                wl[0]++
                wina[0]++
            } else if (a=="C" && b=="B"){
                wl[2]++
                winb[2]++
            } else if(a == "B" && b == "J"){
                wl[2]++
                winb[1]++
            } else if (a=="B" && b == "C"){
                wl[0]++
                wina[2]++
            }
        }
    
        println("${wl[0]} ${wl[1]} ${wl[2]}")
        println("${wl[2]} ${wl[1]} ${wl[0]}")
    
        //b c j 2 0 1
        if (wina[1] > wina[0] && wina[1] > wina[2])
            print('J')
        else if (wina[0] >= wina[1] && wina[0] > wina[2] )
            print('C')
        else print('B')
    
        print(" ")
        
        if (winb[1] > winb[0] && winb[1] > winb[2])
            print('J')
        else if (winb[0] >= winb[1] && winb[0] > winb[2] )
            print('C')
        else print('B')
    
    }
    
    //1019
    
    import java.io.BufferedReader
    import java.io.InputStreamReader
    
    fun main(args: Array<String>) {
        val kin = BufferedReader(InputStreamReader(System.`in`))
        var str = kin.readLine()
        var strs :CharArray
        var num = str.toInt()
        var m: Int
        var n: Int
        var isFirst = true
        while (true) {
            if (!isFirst)
                println()
            else
                isFirst = false
            strs = String.format("%04d",num).toCharArray()
    
            //println(strs)
            strs.sortDescending()
            String(strs).toInt()
            m =  String(strs).toInt()
            strs.sort()
            n =  String(strs).toInt()
            num = m - n
            System.out.printf("%04d - %04d = %04d", m, n, num)
            if (num == 6174 || num == 0) {
                break
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:PAT 乙级(Basic Level)kotlin版 1016-

          本文链接:https://www.haomeiwen.com/subject/juutpktx.html