2014-02-16 2 views
1

나는 기능과 출력 기능을 기반으로 제 1 및 제 2 최고의 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)

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

분명히 프로그램은 Custom을 먼저 출력하지만 Dell이 두 번째라고 말합니다. 나는 아무 소용이없는 모든 시나리오를 포함하려고 시도했다. 미리 감사드립니다.

: 전체 프로그램을 요청하십시오. (이는 제안 된 정렬 방법을 구현합니다. 원본은 위에 게시되었습니다.)

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

대신에 그 클래스에 대한 사용자 정의 클래스와 'Comparator'를 작성하지 않으시겠습니까? – fge

+0

나는 이것을 하나의 클래스에서 만들 것을 요구 받았다 : 나는 왜 프로그램이 비교할 때 'score [0]'을 무시하는지 볼 수 없다. –

+0

글쎄, 알다시피, 동봉 된 클래스가 존재합니다 ... 최종 결과는 여전히 하나의 클래스가 될 것입니다 ... – fge

답변

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

참고 : 이상한 설명은 루프에서 일어나는 일을 따라야합니다. 컬럼으로 해석 (각 컬럼 = 루프의 한 사이클)

+0

haha는 (i가 super derpy를 느꼈던 것처럼) 보였다. 그러나 나는 테스트했다. 그리고 그것은 아직도 일하지 않았다. 내 질문에 설명 된 것과 동일한 입력으로 첫 번째로 사용자 지정을, 두 번째로 Dell을 출력했습니다 (Apple이 두 번째가되어야 함). –

+0

예, 문제는 실제로 이미 첫 번째와 두 번째에있는 값을 비교하지 않는다고 생각합니다. 편집 된 답변보기 – chrizz42

+0

편집 된 코드가 작동하지 않는 이유를 알 수 없지만 프로그램이 잘못된 결과를 내뱉습니다. 전체 코드를보고 싶다면 기꺼이 코드를 공유하십시오. 도움을 주셔서 감사합니다 –

관련 문제