`
superhack
  • 浏览: 32108 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

TCHS-1-250

 
阅读更多

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
 

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[i] < min || speeds[i] > max) {
				bad++;
				continue;
			} else {
				sum += speeds[i];
			}
		}
		return (bad * 10 > speeds.length) ? 0.0 : sum / (speeds.length - bad);
	}
	
}

 

分享到:
评论

相关推荐

    javalruleetcode-Open-Source-Algorithms:快乐编码!

    TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...

    唐亚辉文件关于算法,怎样去实现算法的

    1. **3G移动通信技术概述** - WCDMA (宽带码分多址) - TD-SCDMA (时分同步码分多址) - CDMA2000 (码分多址2000) 2. **C语言编程实践** - 文件包含了C语言代码示例 - 涉及到的基本输入输出操作 - 数学函数的...

    基站代维考试复习题汇总.doc

    1. **FXC E1 板**:FXC E1 板提供4个75欧姆的2M接口,可以作为回路网络的主控或从属方,线路接口4的Rx-connector可作为同步接口接收外部时钟信号。但FXC E1板并不是微波传输设备。 2. **VX系列模块**:FXC E1对应的...

    基站代维考试复习题Nokia设备.docx

    4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...

    Bit Error Rate Testing with CMU300

    BER通常以百分比形式表示,例如1×10^-6表示每百万个比特中有1个比特出错。 #### 支持的测试环境与信道 CMU300支持多种测试环境与信道,包括但不限于: - **上行链路(UL)与下行链路(DL)信号**:包括信道编号...

    TOPCODER 算法PPT1

    【TOPCODER算法PPT1】是一份关于2007年TopCoder竞赛算法讲座的机密文件,它揭示了这个全球知名的编程竞赛平台的核心特点和价值。TopCoder社区是其核心,拥有遍布全球的会员,包括众多活跃的参赛者,涵盖了学生和专业...

    techies:Javascript 验证库。 为什么? 因为我写了一些非常糟糕的验证代码,并且想在学习的同时做一些更健壮的事情

    要告诉技术人员验证元素,请将“tchs”属性添加到元素。 &lt;input type="text" tchs=""&gt;&lt;/input&gt; 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...

Global site tag (gtag.js) - Google Analytics