在一系列值中找到第一个和第二个?

时间:2014-02-16 01:39:32

标签: java arrays sorting

我试图根据函数对各种“pc规格”的值进行排序,并根据函数输出第一和第二好的pc分数:

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

显然,该程序首先输出Custom,但接着说戴尔是第二位。我试图涵盖所有情况,但无济于事。提前谢谢。

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

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();  
}

1 个答案:

答案 0 :(得分:2)

看起来你只是混淆了比较器的标志。

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
            }
        }   
    }

注意:奇怪的评论是遵循循环中发生的事情。将它们解释为colums(其中每列=循环的一个循环)