Reading integers from text files in java and then finding the median -
i'm stuck this, not sure how it.
what want read input set of numbers text file.
for example.
4 1 2 3 4
the first line contains n
, count of numbers. n
lines follow.
if n even, -->n/2
. in case 4/2 = 2. find 2nd smallest number of list. output.
if n odd, --> n+1/2
, , same.
how do this? have far don't know how sort , read array.
i've stumbled way onto this. taking shots in dark.
import java.io.bufferedreader; import java.io.inputstreamreader; class testclass { public static void main(string args[]) throws exception { // read number of data system standard input. bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); string line = br.readline(); int n = integer.parseint(line); // median sum long summedians = 0; { int[] myarray = new int[n]; (int = 0; < n; i++) { myarray[i] = integer.parseint(br.readline()); } } // write output system standard output system.out.println(summedians % 100000); system.out.println(myarray); } }
you can sort list , find medium
arrays.sort(myarray); // use java collection sort summedians = (n % 2 == 0) ? myarray[n/2] : myarray[(n+1)/2];
Comments
Post a Comment