免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 11998 | 回复: 0
打印 上一主题 下一主题

TCHS-1 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-26 14:14 |只看该作者 |倒序浏览

Problem Statement
   
A speed radar is installed in a highway zone where the maximum speed limit is maxLimit mph, and the minimum speed limit is minLimit mph. Any reading that is strictly above or below this interval is an infringement.
Periodically, the radar readings are analyzed to make sure that the sensors are working properly. It is assumed that most drivers obey speed limits, and therefore, the radar will be considered faulty if more than 10% of its readings are infringements.
Given the radar readings over a period of time, return the average speed of all cars that are driving within the speed limits. If the radar is faulty, return 0.0.
Definition
     
Class:
SpeedRadar
Method:
averageSpeed
Parameters:
int, int, int[]
Returns:
double
Method signature:
double averageSpeed(int minLimit, int maxLimit, int[] readings)
(be sure your method is public)
     
Notes
-
The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
-
maxLimit will be between 1 and 200, inclusive.
-
minLimit will be between 1 and maxLimit, inclusive.
-
readings will contain between 1 and 50 elements, inclusive.
-
Each element of readings will be between 1 and 200, inclusive.
Examples
0)
     
1
50
{45, 40, 50}
Returns: 45.0
With all drivers within the speed limits, the return value is just the average speed.
1)
     
1
50
{42,43,44,45,46,47,48,49,50,51}
Returns: 46.0
There is only one driver infringing the limit, and it represents 10% of the total readings. The average speed of the other readings is 46.0.
2)
     
1
50
{42,46,48,50,52}
Returns: 0.0
Only one reading is outside the given limits, but it represents 20% of the total number of readings. We therefore assume that the radar is not working and return 0.0.
3)
     
20
60
{25,45,45,43,24,9,51,55,60,34,61,23,40,40,47,49,33,23,47,54,54}
Returns: 41.68421052631579
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

public class SpeedRadar {

    public double averageSpeed(int min, int max, int speeds[]) {
        int bad = 0;
        double sum = 0.0;
        for (int i = 0; i  speeds.length; i++) {
            if (speeds  min || speeds > max) {
                bad++;
                continue;
            } else {
                sum += speeds;
            }
        }
        return (bad * 10 > speeds.length) ? 0.0 : sum / (speeds.length - bad);
    }
   
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104609/showart_2160910.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP