나는 기능과 출력 기능을 기반으로 제 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();
}
대신에 그 클래스에 대한 사용자 정의 클래스와 'Comparator'를 작성하지 않으시겠습니까? – fge
나는 이것을 하나의 클래스에서 만들 것을 요구 받았다 : 나는 왜 프로그램이 비교할 때 'score [0]'을 무시하는지 볼 수 없다. –
글쎄, 알다시피, 동봉 된 클래스가 존재합니다 ... 최종 결과는 여전히 하나의 클래스가 될 것입니다 ... – fge