gpt4 book ai didi

java - 如何找到数组中最高的 Total value 索引?

转载 作者:行者123 更新时间:2023-12-01 16:36:04 26 4
gpt4 key购买 nike

here fundememtal,database are two subjects. same element's on both arrays are for the same student. ( fundemental[0],database[0]--> this is for the student 1 ) . i need to find highest(high rank) to lowest rank students from this program.i declare a sort method and pass the total array and create that array total as ascending.just check it out my attachment photo. i need to find this rank

here is my code.if anybody have unclear, please ask me.

import java.util.*;
class Remove{
public static void main(String args[]){
int [] fundemental={54,34,35,65,87,37};
int database[]={67,56,45,57,78,89};
int[] total=new int[database.length];

for (int i = 0; i < database.length; i++){
total[i]=fundemental[i]+database[i];
}
int [] arrayTot=sort(total);

int index=0;

for (int i = 0; i < total.length; i++){
for (int j = 0; j < total.length; j++){
if(total[i]==(fundemental[j]+database[j]))
index=i;
}

}
}

public static int[] sort(int[]total){
for (int i = total.length; i >0; i--){
int min=total[0];
int index=0;
for (int j =1; j < i; j++)
{
if(total[j]<min){
min=total[j];
index=j;

}
}
total[index]=total[i-1];
total[i-1]=min;
}
return total;

}
}

if anybody has an another idea for find highest rank to lower rand from student's two subject, please code me.

最佳答案

您可以使用插入排序来处理较小的数组大小并交换所有数组

 public static void main(String[] args) {
int[] fundamentals = { 54, 34, 35, 65, 87, 37 };
int database[] = { 67, 56, 45, 57, 78, 89 };
int len = database.length;
int[] total = new int[len];

for (int i = 0; i < len; i++) {
total[i] = fundamentals[i] + database[i];
}

for (int i = 1; i < len; i++) {
for (int j = i; j > 0 && total[j] > total[j - 1]; j--) {
swap(total, fundamentals, database, j, j - 1);
}
}
}

static void swap(int[] total, int[] fundamentals, int[] database, int i, int j) {
int temp = total[j];
total[j] = total[i];
total[i] = temp;

temp = fundamentals[j];
fundamentals[j] = fundamentals[i];
fundamentals[i] = temp;

temp = database[i - 1];
database[i - 1] = database[i];
database[i] = temp;
}

,输出

[165, 126, 122, 121, 90, 80]
[87, 37, 65, 54, 34, 35]
[78, 89, 57, 67, 56, 45]

关于java - 如何找到数组中最高的 Total value 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61943980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com