找到最大的数字 [英] finding largest numbers
问题描述
我假设1000个数字在一个文件中。我必须在没有排序的情况下找出其中5个b $ b最大的数字。你能给我一个
有效的想法吗?我的想法是将这些数字放入
二叉树并找到最大的数字。我们还能怎样做呢?
问候
Hi,
I have, suppose 1000 numbers, in a file. I have to find out 5
largest numbers among them without sorting. Can you please give me an
efficient idea to do this? My idea is to put those numbers into a
binary tree and to find the largest numbers. How else can we do it?
Regards
推荐答案
qsort& bsearch< stdlib.h中>也许更好。
ramu写道:
qsort & bsearch<stdlib.h> perhaps better.
ramu wrote:
我假设有1000个数字,在一个文件中。我必须找出其中5个最大的数字而不进行排序。你能给我一个有效的想法吗?我的想法是将这些数字放入
二叉树中并找到最大的数字。我们还能做什么呢?
问候
Hi,
I have, suppose 1000 numbers, in a file. I have to find out 5
largest numbers among them without sorting. Can you please give me an
efficient idea to do this? My idea is to put those numbers into a
binary tree and to find the largest numbers. How else can we do it?
Regards
ph ***** @ gmail.com 说:
ramu写道:
<我想,假设1000个数字,在一个文件中。我必须找出其中5个最大的数字而不进行排序。你能给我一个有效的想法吗?我的想法是将这些数字放入
二叉树中并找到最大的数字。我们还能做什么呢?
Hi,
I have, suppose 1000 numbers, in a file. I have to find out 5
largest numbers among them without sorting. Can you please give me an
efficient idea to do this? My idea is to put those numbers into a
binary tree and to find the largest numbers. How else can we do it?
qsort& bsearch< stdlib.h中>也许更好。
qsort & bsearch<stdlib.h> perhaps better.
没有排序的音节是什么你在挣扎吗?
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
Which syllable of "without sorting" were you struggling with?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
ramu说:
ramu said:
假设1000个数字在一个文件中。我必须找出其中5个最大的数字而不进行排序。你能给我一个有效的想法吗?我的想法是将这些数字放入
二叉树中并找到最大的数字。我们还能做什么呢?
Hi,
I have, suppose 1000 numbers, in a file. I have to find out 5
largest numbers among them without sorting. Can you please give me an
efficient idea to do this? My idea is to put those numbers into a
binary tree and to find the largest numbers. How else can we do it?
二叉树基本上是一种排序技术,你说你是
不允许要做。
只需设置一个包含五个数字的数组m,并将它们全部设置为INT_MIN。
然后执行以下操作:
count = 0;
while(successful_got_next_number_in_file_into_n)
{
++ count;
c = 0;
for(j = 0; c == 0&& j< 5; j ++)
{
if(n> m [j])
{
m [j] = n;
c = 1;
}
}
}
if(count< 5)
{
你仍然会在n中有一些INT_MIN条目,在报告程序结果时你应该忽略
。
}
如果你被允许保持m分类,有一种方法可以进一步减少
比较的数量,但我的回答是假设你不允许这样做
任何排序。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
A binary tree would basically be a sorting technique, which you say you''re
not allowed to do.
Just set up an array m of five numbers, and set them all to INT_MIN.
Then do something like this:
count = 0;
while(successfully_got_next_number_in_file_into_n)
{
++count;
c = 0;
for(j = 0; c == 0 && j < 5; j++)
{
if(n > m[j])
{
m[j] = n;
c = 1;
}
}
}
if(count < 5)
{
you will still have some INT_MIN entries in n, which you should disregard
when reporting the results of the program.
}
If you are allowed to keep m sorted, there is a way to reduce the number of
comparisons still further, but my answer assumes you are not allowed to do
any sorting at all.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
这篇关于找到最大的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!