【发布时间】:2014-05-04 02:45:59
【问题描述】:
您好,我目前正在尝试进行中位数的hackerearth 挑战总和,它涉及我从文本文件中读取并将值存储在数组中。第一个值必须存储在变量 N 中,我可以这样做,但其余值必须存储在数组中。这就是我卡住的地方。我必须逐行读取每个值,然后将其存储在数组中。 这是我一直试图让它工作的代码,但我只是看不出我哪里出错了。
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
// read number of data from system standard input.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
int i = 1;
int[] myIntArray = new int[N];
// median sum
long SumMedians = 0;
int median = 0;
while (i<N)
//read one line file and parse as an integer
//store the value in an array
{
myIntArray [i] = Integer.parseInt(line);
i = i + 1; // increment i so i is the total numbers read
}
所以正如我所说,我必须通过将每个值存储在数组中的文本文件递增。任何帮助都会非常感谢
文本文件将如下所示
5
10
5
1
2
15
每行一个字符串,我必须将其传递为整数。 我要做的是将行中的值存储到数组中后,我将对它进行排序并找到它的介质,然后重复此过程,直到读取文本文件中的所有值。
我要解决的问题就是这个
http://www.hackerearth.com/problem/algorithm/sum-of-medians-1/
【问题讨论】:
-
你知道该行的格式吗?每行有 1 个整数吗?每行有多个整数,用空格分隔?每行多个整数,用冒号 (:) 分隔?
-
编辑这篇文章并向我们展示您的文本文件的外观。