HDOJGrey Area

题目链接:HDOJGrey Area
在此转到关于本书博客ACM大学生程序设计竞赛在线题库最新精选题解(赵端阳)部分解析
简单模拟即可,不过样例前两个实际输出应为0.510000,0.260000;另外书中有笔误,不应为图2-1,应该是图2-2。
去零可以乘以10e6模拟,也可以用%g输出,但是此处用printf("%.6lf",sum)也可。
ac代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<string>
using namespace std;
int main() {
    
    
	int n, w;
	int a[100];
	int count[100];
	while (cin >> n >> w && n) {
    
    
		memset(a, 0, sizeof(a));
		memset(count, 0, sizeof(count));
		int maxt = -1;
		int hmx = -1;
		for (int i = 0; i < n; i++) {
    
    
			cin >> a[i];
			a[i] = a[i] / w;
			maxt = max(maxt, a[i]);
		}
		for (int i = 0; i < n; i++) 
			count[a[i]]++;
		for (int i = 0; i <= maxt; i++)
			hmx = max(hmx, count[i]);
		double sum = 0.01;
		for (int i = 0; i < maxt; i++) {
    
    
			double x = (maxt - i)*count[i];
			double y = maxt * hmx;
			sum += x / y;
		}
		printf("%.6lf\n", sum);
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43305312/article/details/109107648