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);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章