- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataflow.java
More file actions
Latest commit
75 lines (68 loc) · 2.24 KB
/
Copy pathDataflow.java
File metadata and controls
75 lines (68 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
packagecom.test;
importjava.util.Iterator;
importjava.util.LinkedList;
importjava.util.Queue;
importjava.util.Scanner;
publicclassDataflow {
/**
* @author wyl
* 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,
* 通过用户访问时间、鼠标轨迹等特征计算用户对于网站的满意程度。
* 现有的统计工具只能统计某一个窗口中,用户的满意程度的均值。
* 夹克老爷想让你为统计工具添加一个新feature,即在统计均值的同时,
* 计算窗口中满意程度的标准差和中位数(均值需要向下取整)。
*/
publicstaticvoidmain(String[] args) {
Dataflownode = newDataflow();
Scannerscan = newScanner(System.in);
intn = scan.nextInt(); //操作次数
intk = scan.nextInt(); //窗口数目
Queue<Integer> queue = newLinkedList<Integer>(); //queue用来控制满意度
LinkedList<Integer> list = newLinkedList<Integer>(); //linkedlist则用来插入排序,方便进行中位数的获取
intsum = 0;
intnumNOW = 0 ;//当前窗口内满意度个数
for(inti=0;i<n;i++){
System.out.println("输入操作类型: 1 用户访问 2 查询均值 3 查询方差 4 查询中位数");
intoperation = scan.nextInt();
switch (operation) {
case1:
System.out.println("输入满意度值");
intsatisfy = scan.nextInt();
sum += satisfy;
numNOW++;
if(queue.size() < k){
queue.offer(satisfy);
node.insertToList(list,satisfy);
}else{
//这里如果队列满了,就会出现覆盖,因为它只保留最近三次(根据窗口数目来定)的访问满意度
intremovedvalue = queue.poll(); //移除并返回队列头部的元素
numNOW--; //满意度窗口数减一
sum -= removedvalue; //总和减去移除队头的元素值
list.remove(Integer.valueOf(removedvalue)); //因为列表是有序的,所以移除这个值
queue.offer(satisfy); //然后 把刚刚进来的满意度值添加进队列尾部
node.insertToList(list, satisfy); //把这个满意度值放到列表指定位置,找到 它的位置排序插入
}
System.out.println(queue+" ..."+list);
break;
case2:
doubleaverage = Math.floor((double)sum/numNOW);
System.out.println(average);
default:
break;
}
}
}
privatevoidinsertToList(LinkedList<Integer> list, intsatisfy) {
Iterator<Integer> it = list.iterator();
intindex = 0 ;
while(it.hasNext()){
intthenumber = it.next();
if(thenumber > satisfy){
break;
}else{
index++;
}
}
list.add(index, satisfy);
}
}