PAT 乙级(Basic Level)kotlin版 1020-1023

//2020 超时2 通过3
//就当学kt的语法了。。。毕竟工作要用

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*


fun main(args: Array) {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val s = br.readLine().split(" ".toRegex()).toTypedArray()
    var count = s[0].toInt()
//NumberFormat 可以很方便的格式化数字,但是一用就超时
//        NumberFormat nf = NumberFormat.getInstance();
//        nf.setMaximumFractionDigits(2);
//        nf.setMinimumFractionDigits(2);
    var tons = s[1].toDouble()
    var result = 0.0
    val ws = br.readLine().split(" ".toRegex()).toTypedArray()
    val ps = br.readLine().split(" ".toRegex()).toTypedArray()
    var w: Double
    var p: Double
    var u: Double
    var flag = false
    val cakeList: MutableList = ArrayList(count)

    //这里如果用sort的话超时更严重
    if (count > 0) {
        //count--;
        while (count > 0) {
            count--
            p = ps[count].toDouble()
            w = ws[count].toDouble()
            u = p / w
            val c = Cake(w, p, u)
            if (cakeList.size == 0) cakeList.add(c) else {
                for (i in cakeList.indices) {
                    if (cakeList[i].unitPrice <= c.unitPrice) {
                        cakeList.add(i, c)
                        flag = true
                        break
                    }
                }
                if (!flag) cakeList.add(c)
            }
            flag = false
        } //end while
    }

    for (cake in cakeList) {
        if (cake.weight <= tons) {
            result += cake.price
            tons -= cake.weight
        } else { //cake.weight > tons
            result += cake.unitPrice * tons
            break
        }
    }
    System.out.printf("%.2f%n", result)
}

class Cake(var weight: Double, var price: Double, unitPrice: Double) :
    Comparable {
    var unitPrice = 0.0

    override fun toString(): String {
        return "Cake{" +
                "unitPrice=" + unitPrice +
                ", weight=" + weight +
                ", price=" + price +
                '}'
    }

    init {
        if (unitPrice == 0.0) {
            this.unitPrice = price / weight
        } else {
            this.unitPrice = unitPrice
        }
    }

    override fun compareTo(other: Cake?): Int {
        if (other == null) return 0

        return if (unitPrice > other.unitPrice) -1 else 1
    }
}

因为kt版超时太严重,所以又写了一遍java版 一样超时

//pat basic 1020 java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main{

    static class Cake implements Comparable {

        double unitPrice;
        double weight;
        double price;

        Cake(double weight,double price,double unitPrice){
            this.weight = weight;
            this.price = price;
            if (unitPrice==0) {
                this.unitPrice = price / weight;
            }else {
                this.unitPrice = unitPrice;
            }
        }

        @Override
        public int compareTo(Cake cake) {
            return this.unitPrice>cake.unitPrice?-1:1;
        }

        @Override
        public String toString() {
            return "Cake{" +
                    "unitPrice=" + unitPrice +
                    ", weight=" + weight +
                    ", price=" + price +
                    '}';
        }
    }


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int count = Integer.parseInt(s[0]);
//        NumberFormat nf = NumberFormat.getInstance();
//        nf.setMaximumFractionDigits(2);
//        nf.setMinimumFractionDigits(2);
        double tons = Double.parseDouble(s[1]);
        double result = 0.0;

        String[] ws = br.readLine().split(" ");
        String[] ps = br.readLine().split(" ");

        double w,p,u;
        List cakeList = new ArrayList<>(count);

        if (count > 0) {
            //count--;
            while (count > 0) {
                count--;
                p = Double.parseDouble(ps[count]);
                w = Double.parseDouble(ws[count]);
                u = p / w;
                Cake c = new Cake(w, p, u);
                cakeList.add(c);
            }//end while
        }
        Collections.sort(cakeList);

//        System.out.println(cakeList);

        for (Cake cake:cakeList){
            if (cake.weight <= tons) {
                result += cake.price;
                tons -= cake.weight;
            }else {//cake.weight > tons
                result+=cake.unitPrice*tons;
                break;
            }
        }
        System.out.printf("%.2f%n", result);
    }
}
//pat 1021

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

fun main(args: Array) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val s = kin.readLine().toCharArray()
//    val result = IntArray(10,0)
    val result = IntArray(10)

    s.forEach {
        val pos = it-'0'
        result[pos]++
    }

    for (i in 0..9){
        if (result[i]!=0)
            println("$i:${result[i]}")
    }

}
//1022
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder

fun main(args: Array) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val s = kin.readLine().split(" ")
    val a = s[0].toInt()
    val b = s[1].toInt()
    val d = s[2].toInt()

    val c = a + b

    print(c.toString(d))
}

//1023

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

fun main(args: Array) {
    val kin = BufferedReader(InputStreamReader(System.`in`))
    val s = kin.readLine().split(" ")
    val arrays = IntArray(10) {
        s[it].toInt()
    }

    val sb = StringBuilder()
    for (i in 1..9) {
        if (arrays[i] != 0) {
            sb.append(i)
            arrays[i]--
            break
        }
    }

    for (i in 0..9) {
        if (arrays[i] != 0) {
            while (arrays[i] > 0) {
                sb.append(i)
                arrays[i]--
            }
        }
    }

    print(sb)
}
result
超时也是没办法的事

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