我的代码有问题。 我的目标是将伪代码转换为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 但我没有得到我的目标。 有人可以指出出了什么问题吗?
谢谢
答案 0 :(得分: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;
}
答案 1 :(得分:0)
看起来您使用studentList.StudentID
的长度而不是studentList
的长度。
查看您的算法,您应该写L1.length
而不是L1.StudentID.length
,L2
也是如此。
编辑:
我刚看到你的编辑。您需要在主for loop
之外取出以下行:
// run the intersection method
int intersection=studentList.intersectionSizeMergeAndSort(firstList,secondList);
System.out.println("The intersection size is: "+intersection);
要正确记录您的时间,您需要在循环之后和我之前提到的前一行之前放置行long startTime = System.currentTimeMillis();
。