如何从文本文件中读取第100个最低编号和第100个最高编号. [英] How to read the 100th least and 100th highest number from a text file.

查看:177
本文介绍了如何从文本文件中读取第100个最低编号和第100个最高编号.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我坚持从包含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分割
将此字符串数组转换为整数数组
对整数数组进行排序
索引为99intArray.Length - 100的项目将为您的结果.
Read the file as String
Split with NewLine
Convert this string array to integer array
Sort the integer array
Items indexed 99 and intArray.Length - 100 will be your result.



  1. 打开文件并假设每个数字都位于单独的行中,逐行读取.
  2. 将文本表示形式转换为数字.
  3. 转换成功后,将数字添加到相应类型的列表中: List<double>
  4. 继续从文件中读取行,直到不再剩余
  5. 通过numbers.Sort()List<double> numbers进行排序.由于数字已经实现了IComparable界面,因此列表将按升序排序.
  6. 第100 个最小元素将位于数字[99] 而第100个 元素将位于 numbers [numbers.Count-99].
  7. 完成!

  1. Open file and read line by line assuming that the each number is on a separate line.
  2. Convert the textual representation to a number. The number format to choose depends on you file content (long, int, float or double).
  3. After successful conversion add the number to a list of the corresponding type: e.g. List<double>
  4. Continue reading lines from your file until there are no more left
  5. Sort the List<double> numbers via numbers.Sort(). Since numbers already implement the IComparable interface the list will be sorted in ascending order.
  6. 100th least element will be at numbers[99] and the 100th highest element will be at numbers[numbers.Count - 99].
  7. Done!



由于任务是查找最小和最大的数字,因此您还需要确保仅在列表中添加一个数字,否则将其添加到列表中.根据文件的内容,这很可能使您拥有不到一百个数字.在那种情况下,对给定的问题没有解决方案.为了避免出现悖论,至少第n个 th 可能小于第n个 th ,您甚至必须检查是否至少有 2n 列表中的元素.因此,在您的情况下,列表长度至少应为200个元素.万一矛盾并不重要,您仍然应该在结果中输出警告.

最好的问候,

—MRB



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,

—MRB


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆