TopCoder02 房间号忘记了。。。JAVA学习笔记02---泛型的应用之分拣数据处理

TopCoder练习题笔记02:


A group of swimmers are stretching along the shores of a river. In just a few moments, they are all going to plunge in, swim downstream (with the current) for a specific distance (unique to each swimmer), and return back (against the current) to their starting points. Relative to a person on the shore, the speed at which each swimmer travels when swimming downstream is the swimmer's speed plus the speed of the current, and the speed at which each swimmer travels when swimming upstream is the swimmer's speed minus the speed of the current. Assume that no time elapses when a swimmer turns around to make the return trip.  Write a class Swimmers with a method getSwimTimes which takes a int[] distances (meters relative to the shore) each swimmer will be swimming, a int[] speeds (meters per second) at which they swim, and an int current representing the speed (meters per second) of the current in the river. Element i in distances corresponds to element i in speeds. The method should return a int[] of the times (rounded down to the nearest integer less than or equal to the actual value) that the roundtrip swim took for each swimmer, or "-1" if the trip is impossible to make. Element i in the returned int[] should correspond to element i in distances and speeds.


题目大概意思:

给出了3个变量,游泳的距离distances,游泳的速度speed,以及水流的速度,让算出对应的时间

顺流的速度为运动员的游泳速度+水流速度

逆流的速度为运动员的游泳速度-水流速度

实现如下:

public int[] getSwimTimes(int[] distances,int[] speeds,int current){
int[] result = new int[speeds.length];
for(int i =0;i<speeds.length;i++){
if(distances[i]==0){
result[i]=0;
}
else if(current>=speeds[i]){
result[i]=-1;
}else{
result[i] =(int)((distances[i]/(float)(speeds[i]+current))
+(distances[i]/(float)(speeds[i]-current)));
}
}
return result;
}

算法很简单,需要考虑的就是游泳速度如果小于水流速度,会造成不能逆流而上;如果输入的距离为0应该也是非法输入需要处理等一些细节需要完善


JAVA学习笔记02---泛型的应用之分拣思路处理:

基本思路如下:

1.对数据做分拣处理,第一次处理未处理过的数据时,要创建一个对象去存放数据

2.如果相同数据类型已经有过存储,就将接下来同样分类的数据存放在已有的对象中

举个例子,最形象的就是快递的分拣

比如现在有很多城市的快递,在分拣时,有北京的快递,天津的,上海的, 深圳的,不同地区的,我们需要先拿一个袋子(创建一个容器)将一个地区的快点放入

然后接续分拣,发现还是北京的快递,就放入到已经创建好的袋子中,如果发现不是北京的快递,比如是上海的,就再创建一个容器用来存放上海的快递,然后再

将快递丢入到袋子中。


一个简单的应用:统计单词出现的次数:

this is a cat and that is a mice and where is the food?

思路如下:

1.分割字符串使用String类的split()方法,当然还有很多方法,比如toCharArray()等

String[] str ="this is a cat and that is a mice and where is the food?".split(" ");


2.用分拣存储思想分拣存储

然后我们创建一个袋子的集合Map

Map <String,Integer> map = new HashMap<String,Integer>();

String用来存储字符串,Integer用来存放次数(这里要注意的是,基本类型不能放入泛型中,需要使用其包装类)


然后我们需要将单词放入袋子

for(String key:str){

}

遍历数组,将数组中单词放入key中,使用增强for循环

在放的时候需要做一个判断,首先如果没有放过单词,那么就需要put进去

使用到的方法有:containsKey()方法用来判断是不是存在相同的键值,返回boolean;put()方法用来将key和value都存入HashMap中

如果袋子中已经有过相同的单词,那么value的值就+1 

map.put(key, map.get(key)+1); get()方法是用来获得Map中key值所对应的Value

以上存储完毕。


3.按要求查看单词出现次数

Set<String> keySet = map.keySet(); 
Iterator<String> it = keySet.iterator();
//以上两个接口对象的转换为了使用迭代器Iterator

while(it.hasNext()){
String key = it.next();
Integer value = map.get(key);
System.out.print(key+"----->"+value+"\t");
}

以上是遍历并显示袋子Map中存放的元素,遍历可以用增强For循环,但是之前学了迭代器,就是用来练练看

首先我们需要把Map转换成Set通过使用Set中的keySet()方法,我的理解是将Map中的key存放到Set中,然后使用Set中的迭代器

创建方法iterator();使用迭代器中的方法hasNext(),Next()来遍历key值,并且通过key获取对应的value,最后打印

问题到这结束,以上是简单的分拣存储的思路。在实际中可能会用到分拣存储+面对对象+HashMap的结合方式去解决一些问题,如:算出一个班中的学生的总分和平均分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值