-2

我的代码有问题。我的目标是将伪代码翻译成 java 代码是的,我的编码是一项作业,我不想要任何答案,只是为了告诉我问题出在哪里

我要做的是计算两个不包含重复的未排序学生列表之间的交集的大小。

我将展示伪代码和我的与此伪代码对应的 java 代码。

伪代码:

inter <-- 0

Array C[m+n]

for i <-- 0 to m-1  do  C[i] <-- A[i]

for i <-- 0 to n-1  do  C[i+m] <-- B[i]

C <-- sort(C, m+n);

pointer <-- 0

while (pointer < m+n-1) do{

if(C[pointer]=C[pointer+1]){

inter <-- inter+1

pointer <-- pointer+2

}

else pointer <-- pointer+1

}

return inter

Java 代码:

public static int intersectionSizeMergeAndSort(studentList L1, studentList L2) {
/* Write your code for question 4 here */
  int intersectionSize = 0;
  int[] C = new int[L1.studentID.length+L2.studentID.length];
  for(int i = 0; i<L1.studentID.length; i++){
  C[i] = L1.studentID[i];
  }
  for(int i = 0; i<L2.studentID.length; i++){
  C[i+L1.studentID.length] = L2.studentID[i];
  }
  Arrays.sort(C);
  int pointer = 0;
  while(pointer<((C.length-1))){
    if(C[pointer] == C[pointer+1]){
    intersectionSize = intersectionSize + 1;
    pointer = pointer + 2;
    }
    else {
      pointer = pointer + 1;
  }
 return intersectionSize;

}
return 0;
}

我的主要方法:

public static void main(String args[]) throws Exception {

studentList firstList;
studentList secondList;

// This is how to read lists from files. Useful for debugging.

// firstList=new studentList("COMP250.txt", "COMP250 - Introduction to Computer Science");
// secondList=new studentList("MATH240.txt", "MATH240 - Discrete Mathematics");

// get the time before starting the intersections
long startTime = System.currentTimeMillis();

// repeat the process a certain number of times, to make more accurate average     measurements.
 for (int rep=0;rep<1000;rep++) {

 // This is how to generate lists of random IDs. 
 // For firstList, we generate 16000 IDs
 // For secondList, we generate 16000 IDs

 firstList=new studentList(2 , "COMP250 - Introduction to Computer Science");
 secondList=new studentList(2 , "MATH240 - Discrete Mathematics");


 // run the intersection method
 int intersection=studentList.intersectionSizeMergeAndSort(firstList,secondList);
 System.out.println("The intersection size is: "+intersection);
 }

// get the time after the intersection
long endTime = System.currentTimeMillis();


System.out.println("Running time: "+ (endTime-startTime) + " milliseconds");
 }

}

注意:L1 和 L2 之前已声明但我没有得到我想要的结果。有人能指出什么是错的吗?

谢谢

4

2 回答 2

1

在我的脑海中,在我看来,您的return intersectionSize;语句出现在您的 while 循环中,因此您的循环永远不会超出第一次迭代,也不会正确计算 intersectionSize。我会删除该声明并return 0;return intersectionSize;这样的方式替换您的...

public static int intersectionSizeMergeAndSort(studentList L1, studentList L2) {
   /* Write your code for question 4 here */
   int intersectionSize = 0;
   int[] C = new int[L1.studentID.length + L2.studentID.length];
   for (int i = 0; i < L1.studentID.length; i++) {
      C[i] = L1.studentID[i];
   }
   for (int i = 0; i < L2.studentID.length; i++) {
      C[i + L1.studentID.length] = L2.studentID[i];
   }
   Arrays.sort(C);
   int pointer = 0;
   while (pointer < (C.length - 1)) {
       if (C[pointer] == C[pointer + 1]) {
          intersectionSize = intersectionSize + 1;
          pointer = pointer + 2;
       } else {
          pointer = pointer + 1;
       }
    }
    return intersectionSize;
}
于 2013-01-22T18:05:10.903 回答
0

看起来您使用的是长度studentList.StudentID而不是长度studentList

查看您的算法,您应该编写L1.lengthL1.StudentID.length不是L2.

编辑:

我刚看到你的编辑。您需要在 main 之外取出以下几行for loop

// run the intersection method
   int intersection=studentList.intersectionSizeMergeAndSort(firstList,secondList);
   System.out.println("The intersection size is: "+intersection);

为了正确记录您的时间,您需要将行long startTime = System.currentTimeMillis();放在循环之后和我提到的前面几行之前。

于 2013-01-22T17:58:46.530 回答