codeforces 解题报告 1003C. Intense Heat 暴力

http://codeforces.com/problemset/problem/1003/C

解题思路:

1.给出n天的气候值,求连续小于等于k天的气候最大平均值

2.数据范围n<=5000,直接暴力枚举所有情况即可,答案误差1e-6,直接double

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(),k = sc.nextInt();
        int[] tem = new int[5005];
        for(int i = 1;i <= n;i++) {
            tem[i] = sc.nextInt();
        }
        
        double ans = 0;
        for(int i = 1;i <= n - k + 1;i++) {
            double sum = 0;
            int j = 0;
            for(j = i;j < i + k - 1;j++) {
                sum += tem[j];
            }
            for(j = i + k - 1;j <= n;j++) {
                sum += tem[j];
                ans = Math.max(ans,sum / (j - i + 1));
            }
        }
        
        System.out.print(ans);
    }
}

猜你喜欢

转载自blog.csdn.net/a912952381/article/details/81028564