No older revisions available
No older revisions available
Number.java ¶
~cpp
import java.util.Scanner;
public class Number
{
final int MAX_SIZE = 100000;
final int SENTINEL_NUM = -999;
private int elements[];
private int length;
public Number()
{
elements = new int[MAX_SIZE];
length = 0;
}
public void inputNumber()
{
Scanner input = new Scanner(System.in);
for (int i = 0; i < MAX_SIZE; i++)
{
elements[i] = input.nextInt();
if (elements[i] == SENTINEL_NUM)
break;
else
length++;
}
}
public int findMidiumNumber()
{
sortElement(0, length);
int mid = (int)Math.floor(0.5 * length + 0.5);
return elements[mid - 1];
}
public void sortElement(int start, int end)
{
int min;
for (int i = start; i < end - 1; i++)
{
min = i;
for (int j = i + 1; j < end; j++)
{
if (elements[min] > elements[j])
min = j;
}
swapElement(i, min);
}
}
public void swapElement(int i, int j)
{
int t = elements[i];
elements[i] = elements[j];
elements[j] = t;
}
}
~cpp TestNumber.java
¶
~cpp
public class testNumber
{
public static void main(String args[])
{
Number number = new Number();
number.inputNumber();
int midNum = number.findMidiumNumber();
System.out.println(midNum);
}
}