问题描述
目前,我正在为Android开发一个应用程序,偶然发现了一个问题-我有12分(A, B, C, D, E, F, G, Q, N, L, M and K)
。
我需要做一些6分最高的东西。
为了进行计算,我编写了以下代码:
List<Integer> YPoint = new java.util.ArrayList(Arrays.asList(A.y, B.y, C.y, D.y, E.y, F.y, G.y, K.y, Q.y, L.y, M.y, N.y));
List<Integer> YUpper = new java.util.ArrayList(Arrays.asList());
int Classified = 0;
int Highest = 0;
while(Classified != 6){
Highest = Collections.min(YPoint);
YPoint.remove(Integer.valueOf(Highest));
YUpper.add(Integer.valueOf(Highest));
Classified++;
}
android.graphics.Point Highest1 = new android.graphics.Point();
android.graphics.Point Highest2 = new android.graphics.Point();
android.graphics.Point Highest3 = new android.graphics.Point();
android.graphics.Point Highest4 = new android.graphics.Point();
android.graphics.Point Highest5 = new android.graphics.Point();
android.graphics.Point Highest6 = new android.graphics.Point();
boolean notdone = false;
if(YUpper.contains(A.y)){
if(Highest1.x == 0 && !notdone){Highest1 = A; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = A; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = A; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = A; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = A; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = A; notdone = true;}
notdone = false;
}
if(YUpper.contains(B.y)){
if(Highest1.x == 0 && !notdone){Highest1 = B; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = B; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = B; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = B; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = B; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = B; notdone = true;}
notdone = false;
}
if(YUpper.contains(C.y)){
if(Highest1.x == 0 && !notdone){Highest1 = C; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = C; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = C; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = C; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = C; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = C; notdone = true;}
notdone = false;
}
if(YUpper.contains(E.y)){
if(Highest1.x == 0 && !notdone){Highest1 = E; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = E; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = E; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = E; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = E; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = E; notdone = true;}
notdone = false;
}
if(YUpper.contains(D.y)){
if(Highest1.x == 0 && !notdone){Highest1 = D; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = D; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = D; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = D; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = D; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = D; notdone = true;}
notdone = false;
}
if(YUpper.contains(F.y)){
if(Highest1.x == 0 && !notdone){Highest1 = F; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = F; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = F; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = F; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = F; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = F; notdone = true;}
notdone = false;
}
if(YUpper.contains(G.y)){
if(Highest1.x == 0 && !notdone){Highest1 = G; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = G; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = G; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = G; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = G; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = G; notdone = true;}
notdone = false;
}
if(YUpper.contains(Q.y)){
if(Highest1.x == 0 && !notdone){Highest1 = Q; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = Q; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = Q; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = Q; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = Q; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = Q; notdone = true;}
notdone = false;
}
if(YUpper.contains(K.y)){
if(Highest1.x == 0 && !notdone){Highest1 = K; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = K; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = K; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = K; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = K; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = K; notdone = true;}
notdone = false;
}
if(YUpper.contains(N.y)){
if(Highest1.x == 0 && !notdone){Highest1 = N; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = N; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = N; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = N; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = N; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = N; notdone = true;}
notdone = false;
}
if(YUpper.contains(L.y)){
if(Highest1.x == 0 && !notdone){Highest1 = L; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = L; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = L; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = L; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = L; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = L; notdone = true;}
notdone = false;
}
if(YUpper.contains(M.y)){
if(Highest1.x == 0 && !notdone){Highest1 = M; notdone = true;}
if(Highest2.x == 0 && !notdone){Highest2 = M; notdone = true;}
if(Highest3.x == 0 && !notdone){Highest3 = M; notdone = true;}
if(Highest4.x == 0 && !notdone){Highest4 = M; notdone = true;}
if(Highest5.x == 0 && !notdone){Highest5 = M; notdone = true;}
if(Highest6.x == 0 && !notdone){Highest6 = M; notdone = true;}
notdone = false;
}
YPoint.remove(Integer.valueOf(Highest1.y));
YPoint.remove(Integer.valueOf(Highest2.y));
YPoint.remove(Integer.valueOf(Highest3.y));
YPoint.remove(Integer.valueOf(Highest4.y));
YPoint.remove(Integer.valueOf(Highest5.y));
YPoint.remove(Integer.valueOf(Highest6.y));
简而言之,我的代码遵循以下算法:
-
列出12点的
Y
坐标 -
从列表中获取6个具有最小
Y
坐标的int
(对应于最高) - 从2查找对应于这6个整数的点
- 新建6个点并将其坐标设置为3个点-我复制了点,以便可以在进一步的计算中使用它们。
上面的代码可以正常工作,但是它又庞大又混乱。 是否有任何良好而干净的替代方法? 提前致谢。
1楼
Tomás Ruiz-López
0
2015-07-26 13:30:57
我现在无法提供代码,但是您需要的算法如下:
- 用比较器将12个点放入SortedSet(其他类也可能有用)中,以y最低的顺序对其进行排序。
- 首先在SortedSet中弹出6,然后将其存储到列表中。
查看API文档,它们非常简单。