anyone can helping with comp1405(Java) assignment??

几分之几

新手上路
注册
2003-12-05
消息
2,968
荣誉分数
0
声望点数
0
实在写不下去了,卡住了,有没有高手能帮忙指点一下?


Q2. Write the method public int[] partialSort(int[] anArray) which takes an array of int as a parameter and returns a new array of int. The new array should contain at the front all the odd numbers from the given array and all the even numbers of that array at the end. For example, if we are given an array with the numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 then the resulting array should contain numbers as follows: 1, 3, 5, 7, 9, 2, 4, 6, 8, 10. Notice that all the odd numbers are in the first half of the array and the even numbers in the second half. Note however, that there may be no odd numbers or possibly no even numbers. You may assume that there is at least one number in the given array.

(Hint: you can use the modulus (%) operator to decide if something is odd/even).

Write the above method in a SortTester class with a public static void main() method that tests the partialSort() method with the following arrays:

{1,2,3,4,5,6,7,8,9,10}

{8}

{5}

{9,7,5}

{98, 8}

{12, 57, 45, 9, 354, 3, 2, 56, 7,8 ,94, 45,67, 21}

Copy and paste your testing for marking. Your main method should prompt the user as follows:



Please enter number of elements in the array: 3

Please enter 1 element: 1

Please enter 2 element: 4

Please enter 3 element: 5



You entered the array: 1, 4, 5

Partially sorted as : 1, 5, 4



Note that you do not have to sort the input array.
 
int[] toReturn = new int[anArray.length];

for i=0; i< anArray.length ; i++ {
int endIndex = (anArray.length-i);
if (anArray%2 ==1) {toReturn=anArray;}
else {toReturn[endIndex ]=anArray;}
}
return toReturn[];


//if there is a compile error, try to debug yourself. I already give you the algorithm
 
Java and C++ looks same. Java has more API and is well structured on its library, but real time performance is not good, because Java has an auto garbage collector.

For C++, there isn't. You have to do dynamic memory allocation/destruction on your own when using C++.

Overall speaking, C++ is far more useful on software development than Java. C++ is the key. Try to learn it better.
 
The most efficient way to figure out the odd or event is not using the "%" operator, but use bit and.
if ((anyNumber & 1) != 0){
//odd number
}
else{
// even number
}

% will require more calculation internally (probably 4-5 more CPU commands)
 
P.S. 你那张做头像的日图,怎么和我的一个那么像???哪儿照的?
 
最初由 闲丸 发布
The most efficient way to figure out the odd or event is not using the "%" operator, but use bit and.
if ((anyNumber & 1) != 0){
//odd number
}
else{
// even number
}

% will require more calculation internally (probably 4-5 more CPU commands)

这个正解
 
In math:

z is an integer, z in congruence class of 2

z = 0 (mod 2) z is set of even#
z = 1 (mod 2) z is set of odd#.


This is some Lemma in abstract algebra.

% operator, I am not too sure how engineer construct it. And if it's 100% satisfy the above math Lemma.
 


你也上这个啊

good luck!!!

上个fall term 我被这个折磨的头痛

总算是混过去了
 
后退
顶部