PAT_B_1053_Java(20分)

超时的测试点多提交几次就过了

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");	// N,e,D
        int N = Integer.parseInt(str[0]);			// 住房总套数
        double e = Double.parseDouble(str[1]);		// 低电量阈值
        int D = Integer.parseInt(str[2]);			// 观察期阈值

        int maybeVacancy = 0, vacancy = 0;			// “可能空置”住房数,“空置”住房数
        for (int i = 0; i < N; ++i) {
            str = br.readLine().split(" ");
            int K = Integer.parseInt(str[0]);		// 观察的天数(观察期)
            int num = 0;							// 第i套住房的低于阈值e的天数
            for (int j = 1; j <= K; ++j) {			// K天
                double E = Double.parseDouble(str[j]);// 第j天的用电量
                if (E < e) {						// 低于阈值
                    ++num;							// 天数加1
                }
            }
            if (num > (K / 2)) {					// 低于阈值的天数超过观察期的一半
                if (K > D) {						// 观察期超过观察期阈值
                    ++vacancy;						// “空置”住房数加1
                } else {
                    ++maybeVacancy;					// “可能空置”住房数加1
                }
            }
        }
        System.out.printf("%.1f", maybeVacancy * 100.0 / N);// 一位小数
        System.out.print("% ");
        System.out.printf("%.1f", vacancy * 100.0 / N);
        System.out.println("%");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43511405/article/details/107380426