gpt4 book ai didi

java - 在值数组中查找第一个和第二个?

转载 作者:行者123 更新时间:2023-11-30 04:01:47 24 4
gpt4 key购买 nike

我正在尝试根据函数对各种“电脑规范”的值进行排序,并根据该函数输出第一和第二最佳的电脑分数:

score[i] = 2*pcram[i] + 3*pccpu[i] + pchdd[i];

排序在这里:

    //place sorted data into seperate arrays to make calculations more logical to look at
for (i = 0; i < numpc; i++){

pcram[i] = Integer.parseInt(pcname[i][1]);
pccpu[i] = Integer.parseInt(pcname[i][2]);
pchdd[i] = Integer.parseInt(pcname[i][3]);

}

//solve the score and find first and second place
for (i = 0; i < numpc; i++){
score[i] = 2*pcram[i] + 3*pccpu[i] + pchdd[i];
}

for (i = 0; i < numpc - 1; i++){
if (i == 0 && score[i + 1] > score[i]){
first = i + 1;
second = i;
}
if(i == 0 && score[i + 1] > score[i]){
first = i;
second = i+1;
}
if (score[i + 1] > score[i]){
second = first;
first = i+1;
}
if (score[i] > score[i+1]){
second = first;
first = i;
}
}
System.out.println(pcname[first][0] + " " + score[first]);
System.out.println(pcname[second][0] + " " + score[second] + " " + score[0]);

导致错误的示例输入是:

(输入内容如下:PC数量、PC名称、RAM、CPU、HDD)

4
Apple 16 3 500
Dell 16 2 500
HP 8 2 500
Custom 1000 1000 1000

显然,程序输出“自定义”为第一,但随后显示“戴尔”为第二。我试图涵盖所有场景但无济于事。提前致谢。

编辑:根据要求,完整的程序。 (这里实现了建议的排序方法,原文贴在上面)

public static void main(String[] args) {

Scanner nameinput = new Scanner(System.in);
Scanner datainput = new Scanner(System.in);

int numpc = datainput.nextInt();

String[][] pcname = new String[numpc][5]; //hold sorted data

String[] pcdata = new String[numpc]; //hold unsorted data

int i = 0;
int first = 0;
int second = 0;

int[] score = new int[numpc];
int[] pcram = new int[numpc];
int[] pccpu = new int[numpc];
int[] pchdd = new int[numpc];

//begin program
for (i = 0; i < numpc; i++){

pcdata[i] = nameinput.nextLine(); //get unsorted data

}

for (i = 0; i < numpc; i++){

pcname[i] = pcdata[i].split(" "); //sort data

}

//place sorted data into seperate arrays to make calculations more logical to look at
for (i = 0; i < numpc; i++){

pcram[i] = Integer.parseInt(pcname[i][1]);
pccpu[i] = Integer.parseInt(pcname[i][2]);
pchdd[i] = Integer.parseInt(pcname[i][3]);

}

//solve the score and find first and second place
for (i = 0; i < numpc; i++){
score[i] = 2*pcram[i] + 3*pccpu[i] + pchdd[i];
}

for(i = 0; i<score.length-1; i++){ //first find and store the highest values
if(score[i]> score[i+1]){
if(score[i]>first){
first = score[i];
}
if(score[i+1]>second){
second = score[i+1];
}
} else {
if(score[i+1]>first){
first = score[i+1];
}
if(score[i]>second){
second = score[i];
}
}
}
for(i= 0; i<score.length; i++){ //now get the index of that value
if(first == score[i]){
first = i;
break;
}
}
for(i= 0; i<score.length; i++){ //index for second
if(second == score[i]){
second = i;
break;
}
}
System.out.println(pcname[first][0] + " " + score[first]);
System.out.println(pcname[second][0] + " " + score[second] + " " + score[0]);
nameinput.close();
datainput.close();
}

最佳答案

看起来您只是混淆了比较器符号。

for (i = 0; i < numpc - 1; i++){
if (i == 0 && score[i + 1] > score[i]){
first = i + 1;
second = i;
}
if(i == 0 && score[i + 1] < score[i]){ //<--- you had > here
first = i;
second = i+1;
}
if (score[i + 1] > score[i]){
second = first;
first = i+1;
}
if (score[i] > score[i+1]){
second = first;
first = i;
}
}

*编辑*我认为问题是你不比较 i 的值实际上大于还是小于第一个或第二个值,你应该尝试这个:

    int first = 0, second = 0;
for(i = 0; i<score.length-1; i++){ //first find and store the highest values
if(score[i]> score[i+1]){
if(score[i]>first){
second = first; // *NEW* the former first place is now the second !
first = score[i]; //first = 541 //NAN // NAN
}
if(score[i+1]>second){ //second = 538 //NAN // NAN
second = score[i+1];
}
} else {
if(score[i+1]>first){
second = first;
first = score[i+1];//NAN // 522< 541 // first = 6000
}
if(score[i]>second){
second = score[i];//NAN // NAN // NAN
}
}
}

注意:奇怪的注释是跟在循环中发生的事情之后的。将它们解释为列(其中每一列 = 一个循环周期)

关于java - 在值数组中查找第一个和第二个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21806158/

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