如何从文本文件中读取第100个最低编号和第100个最高编号. [英] How to read the 100th least and 100th highest number from a text file.
问题描述
大家好,
我坚持从包含1000个随机数的文本文件中找到第100位和第100位最高的数字.所以请任何人帮我解决这个问题.
任何帮助都将受到高度赞赏.
问候
Abhishek
Hello Everyone,
I am stuck at finding the 100th least and 100th highest number from a text file containing 1000 random numbers. So please could anyone help me out with this issue.
Any kind of help is highly appreciated.
Regards
Abhishek
推荐答案
以字符串形式读取文件
用NewLine分割
将此字符串数组转换为整数数组
对整数数组进行排序
索引为99
和intArray.Length - 100
的项目将为您的结果.
Read the file as String
Split with NewLine
Convert this string array to integer array
Sort the integer array
Items indexed99
andintArray.Length - 100
will be your result.
- 打开文件并假设每个数字都位于单独的行中,逐行读取.
- 将文本表示形式转换为数字.
- 转换成功后,将数字添加到相应类型的列表中:
List<double>
- 继续从文件中读取行,直到不再剩余
- 通过
numbers.Sort()
对List<double> numbers
进行排序.由于数字已经实现了IComparable
界面,因此列表将按升序排序. - 第100 个最小元素将位于数字[99] 而第100个 元素将位于 numbers [numbers.Count-99].
- 完成!
- Open file and read line by line assuming that the each number is on a separate line.
- Convert the textual representation to a number. The number format to choose depends on you file content (long, int, float or double).
- After successful conversion add the number to a list of the corresponding type: e.g.
List<double>
- Continue reading lines from your file until there are no more left
- Sort the
List<double> numbers
vianumbers.Sort()
. Since numbers already implement theIComparable
interface the list will be sorted in ascending order. - 100th least element will be at numbers[99] and the 100th highest element will be at numbers[numbers.Count - 99].
- Done!
由于任务是查找最小和最大的数字,因此您还需要确保仅在列表中添加一个数字,否则将其添加到列表中.根据文件的内容,这很可能使您拥有不到一百个数字.在那种情况下,对给定的问题没有解决方案.为了避免出现悖论,至少第n个 th 可能小于第n个 th ,您甚至必须检查是否至少有 2n 列表中的元素.因此,在您的情况下,列表长度至少应为200个元素.万一矛盾并不重要,您仍然应该在结果中输出警告.
最好的问候,
Since the task was to find the least and highest number you''d also need to make sure that you only add a number to the list if it Contains
it not. Depending on the content of your file this may very well leave you with less than a hundred numbers. In that case there is no solution to the given question. To also avoid the paradoxon that the nth highest may be smaller than the nth least you''d even have to check that there are at least 2n elements in the list. In your case the list should thus be at least 200 elements long. In case the paradox doesn''t really matter you should still output a warning with your result.
Best Regards,
0)加载文件.
1)将定界符上的字符串(空格,逗号等)拆分为字符串数组.
2)遍历数组以找到最大值.
0) Load the file.
1) Split the string on the delimiter (a space, comma, whatever) into an array of strings.
2) Loop through the array to find the highest value.
这篇关于如何从文本文件中读取第100个最低编号和第100个最高编号.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!