英文:
Sort an array From highest to lowest
问题
我正在尝试在Java中对数组进行排序,我只想要前3个最大的值。
// A:55,B:45,C:5,D:35,E:35,F:1
int a[] = {A,B,C,D,E,F};
float first =0;
float second=0;
float third =0;
for (int i=0; i<a.length; i++){
if(first <= a[i]){
first=a[i];
}
}
System.out.println("first largest is "+first);
for (int j=0; j<a.length; j++){
if(a[j] <= first && a[j] > second){
second = a[j];
}
}
System.out.println("second largest is "+second);
for (int k=0;k<a.length; k++){
if(a[k]<=second && a[k]>third){
third =a[k];
}
}
System.out.println("third largest is "+third);
另一件事是,如果A和B相等,它应该给我A,如果D和E相等,它应该给我D作为答案。因此,最终答案应该是ABD。
英文:
I am trying to sort an array in Java, I want top 3 value only.
// A:55,B:45,C:5,D:35,E:35,F:1
int a[] = {A,B,C,D,E,F};
float first =0;
float second=0;
float third =0;
for (int i=0; i<a.length; i++){
if(first <= a[i]){
first=a[i];
}
}System.out.println("first largest is "+first);
for (int j=0; j<a.length; j++){
if(a[j] <=first && a[j] > second){
second = a[j];
}
}System.out.println("second largest is "+second);
for (int k=0;k<a.length; k++){
if(a[k]<=second && a[k]>third){
third =a[k];
}
}System.out.println("third largest is "+third);
Another thing here is that if A and B are equal, it should give me A and if D and E are equal it should provide D as answer. So final answer should be ABD.
答案1
得分: 1
你可以将索引存储在变量first、second、third中,而不是存储值。
我快速地重写了代码,虽然不是最优的,但我认为它可以帮助。
public static void main(String[] args) {
char names[] = {'A', 'B', 'C', 'D', 'E', 'F'};
int values[] = {55, 45, 5, 35, 35, 1, Integer.MIN_VALUE};
int first = values.length - 1;
int second = values.length - 1;
int third = values.length - 1;
for (int i = 0; i < values.length; i++) {
if (values[first] < values[i]) {
first = i;
}
}
System.out.println("first largest is " + names[first]);
for (int j = 0; j < values.length; j++) {
if (j != first && values[second] < values[j]) {
second = j;
}
}
System.out.println("second largest is " + names[second]);
for (int k = 0; k < values.length; k++) {
if (k != first && k != second && values[third] < values[k]) {
third = k;
}
}
System.out.println("third largest is " + names[third]);
}
我在列表末尾添加了Integer.MIN_VALUE,以将其作为变量first、second和third的初始索引,如果需要,你可以将其替换为0,但它将不适用于负数。
英文:
you can store in the variable first,second,third the index instead of value
I rewrite the code quickly it not optimal but I think it can help
public static void main(String[] args) {
char names[] = {'A', 'B', 'C', 'D', 'E', 'F'};
int values[] = {55, 45, 5, 35, 35, 1, Integer.MIN_VALUE};
int first = values.length - 1;
int second = values.length - 1;
int third = values.length - 1;
for (int i = 0; i < values.length; i++) {
if (values[first] < values[i]) {
first = i;
}
}
System.out.println("first largest is " + names[first]);
for (int j = 0; j < values.length; j++) {
if (j != first && values[second] < values[j]) {
second = j;
}
}
System.out.println("second largest is " + names[second]);
for (int k = 0; k < values.length; k++) {
if (k != first && k != second && values[third] < values[k]) {
third = k;
}
}
System.out.println("third largest is " + names[third]);
}
I added the Integer.MIN_VALUE to the end of list to be as the initial index for the variable first ,second and third you cad replace it with 0 if you need but it will never work with negative number then
答案2
得分: 1
[最大堆][1] 数据结构是你的朋友:
public static void main(String[] args) {
char[] arr = { 'A', 'B', 'C', 'D', 'E', 'F' };
List<Character> sorted = sortAndGetMethodTopThree(arr);
System.out.println("第一个最大的是 " + sorted.get(0));
System.out.println("第二个最大的是 " + sorted.get(1));
System.out.println("第三个最大的是 " + sorted.get(2));
}
public static List<Character> sortAndGetMethodTopThree(char... arr) {
Map<Character, Integer> map = Map.of('A', 55, 'B', 45, 'C', 5, 'D', 35, 'E', 35, 'F', 1);
Comparator<Character> sortByValueAndLetterDesc = (one, two) -> {
int res = Integer.compare(map.get(two), map.get(one)); // 首先按数字降序排序
return res == 0 ? Character.compare(one, two) : res; // 其次按字母升序排序
};
Queue<Character> maxHeap = new PriorityQueue<>(sortByValueAndLetterDesc);
for (char ch : arr)
maxHeap.add(ch);
List<Character> res = new ArrayList<>(3);
for (int i = 0; i < 3 && !maxHeap.isEmpty(); i++)
res.add(maxHeap.remove());
return res;
}
[1]: https://en.wikipedia.org/wiki/Min-max_heap
英文:
Max Heap data structure is your friend:
public static void main(String[] args) {
char[] arr = { 'A', 'B', 'C', 'D', 'E', 'F' };
List<Character> sorted = sortAngGetTopThree(arr);
System.out.println("first largest is " + sorted.get(0));
System.out.println("second largest is " + sorted.get(1));
System.out.println("third largest is " + sorted.get(2));
}
public static List<Character> sortAngGetTopThree(char... arr) {
Map<Character, Integer> map = Map.of('A', 55, 'B', 45, 'C', 5, 'D', 35, 'E', 35, 'F', 1);
Comparator<Character> sortByValueAndLetterDesc = (one, two) -> {
int res = Integer.compare(map.get(two), map.get(one)); // sort by number desc first
return res == 0 ? Character.compare(one, two) : res; // sort by letter asc second
};
Queue<Character> maxHeap = new PriorityQueue<>(sortByValueAndLetterDesc);
for (char ch : arr)
maxHeap.add(ch);
List<Character> res = new ArrayList<>(3);
for (int i = 0; i < 3 && !maxHeap.isEmpty(); i++)
res.add(maxHeap.remove());
return res;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论