从最高到最低排序 [英] sort a number highest to lowest

查看:93
本文介绍了从最高到最低排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试创建一个函数,将数字的数字从最高到最低排序为
。 />

例如


1000 - 将变为0001

或1234至4321


最好先将数字转换为字符串然后排序吗?或者

还有更好的方法吗?

我只需要一些关于如何做的线索。我试图搜索一些

材料,但是我只看到了如何排序''LIST''而不是

只是一个数字。

感谢您的帮助

解决方案

rh ******* @ gmail.com 写道:





我正在尝试创建一个函数来对数字的数字进行排序

从最高到最低。


例如


1000 - 将变为0001


或1234至4321


更好地隐蔽首先是字符串的数字然后排序?或者

还有更好的方法吗?



再想一想你要排序的内容 - 二进制数的小数

表示。所以你没有对数字进行排序,

但是它的文字表示。文本表示是.....


我只需要一些关于如何做的线索。我试图搜索一些

材料,但是我只看到了如何排序''LIST''而不是

只是一个数字。



如果你考虑要排序的东西,你会看到清单。


-

Ian Collins。


rh ******* @ gmail.com 说:





我正在尝试创建一个能够排序一个数字的数字

从最高到最低。


例如


1000 - 将成为0001


或1234到4321


首先将数字转换为字符串然后排序是否更好?或者

还有更好的方法吗?



Buckets,亲爱的朋友!水桶!世界上最快的分拣技术这是Lucksort的
方面!


我只需要一些关于如何做的线索。我试图搜索一些

材料,但是我只看到了如何排序''LIST''而不是

只是一个数字。



假设您只处理基数N的数字,您可以定义一个N位数的

数组,并将其清除为全部0。如果N是一个常数整数

表达式(或者你使用的是C99)你可以这样做:


无符号长数[N] = {0} ;


现在遍历数字,用N修改。让我们第一次处理

傻事:


if(n == 0)

{

++ digit [0];

}

else

{

/ *现在我们算上数字* /

而(数字0)

{

++数字[n%N];

数字/ = N;

}

}


你现在有你的数字计数,它只是迭代你的

数组,打印出最适合你的数字,

确保每次打印适当的次数。


-

理查德希思菲尔德

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


< rh ******* @ gmail .comwrote in message

news:11 ********************** @ 80g2000cwy.googlegro ups.com ...





我正在尝试创建一个可以对数字进行排序的函数

从最高到最低。


例如


1000 - 将变为0001



你的意思是1000会留1000吗?


或1234到4321


更好地隐藏首先编号为字符串然后排序?或者

还有更好的方法吗?

我只需要一些关于如何做的线索。我试图搜索一些

材料,但是我只看到了如何排序''LIST''而不是

只是一个数字。

感谢您的帮助



有许多解决方案。如果你不喜欢排序太多,并且你的

数是一个整数,你可以随时执行以下操作:如果你的数字在

base N,然后创建一个N个元素长的整数数组(最初

所有元素都是0)。


现在取你的数字并做一个mod N on它。结果是

数字的最后一位数。假设数字是X.将数组的第X个元素增加1.

现在将原始数字除以N.由于你的数字和N都是整数,

你要做的就是删除最后一位数字。现在对其他数字执行相同的操作

循环,直到你的数字剩下的是0.


现在你将拥有一个数组元素告诉你每个数字有多少

。将Y定义为整数并将其设置为1.遍历数组。如果

当前元素为0,则将其减1并执行:


结果+ = X * Y;

Y * = N;


其中X是数组的当前元素(不是它的值,但它在

中的位置是该数组)。我假设结果在此之前等于0。这样做直到X-th
元素为=。

完成这两个循环后,变量''result''将包含你的结果。


-

世界上很容易生活在世界的意见之后;孤独轻松

住在我们自己之后;但伟大的人就是在人群中间的人。

保持完美的甜蜜独处的孤独。

Ralph Waldo Emerson,自力更生1841年/> http://pinpoint.wordpress.com/


Hi,

I''m trying to create a function that will sort a number''s digits
from highest to lowest.

For example

1000 - will become 0001

or 1234 to 4321

Is it better to covert the number to a string first then sort? or
is there a better way to do it?
I just need some clues on how to do it. I''ve tried to search some
materials on how to do it but I only saw how to sort a ''LIST'' but not
just a number.
Thanks for the help

解决方案

rh*******@gmail.com wrote:

Hi,

I''m trying to create a function that will sort a number''s digits
from highest to lowest.

For example

1000 - will become 0001

or 1234 to 4321

Is it better to covert the number to a string first then sort? or
is there a better way to do it?

Think again about what you are attempting to sort - the decimal
representation of a binary number. So you aren''t sorting the number,
but a textual representation of it. The textual representation is a .....

I just need some clues on how to do it. I''ve tried to search some
materials on how to do it but I only saw how to sort a ''LIST'' but not
just a number.

If you consider what you are sorting, you will see the list.

--
Ian Collins.


rh*******@gmail.com said:

Hi,

I''m trying to create a function that will sort a number''s digits
from highest to lowest.

For example

1000 - will become 0001

or 1234 to 4321

Is it better to covert the number to a string first then sort? or
is there a better way to do it?

Buckets, my dear chap! Buckets! The world''s fastest sorting technique this
side of Lucksort!

I just need some clues on how to do it. I''ve tried to search some
materials on how to do it but I only saw how to sort a ''LIST'' but not
just a number.

Presuming you are dealing only with digits in base N, you can define an
array of N digits, and clear it down to all 0s. If N is a constant integer
expression (or if you are using C99) you can do this:

unsigned long digit[N] = {0};

Now iterate through the number, modding with N. Let''s first deal with the
silly case:

if(n == 0)
{
++digit[0];
}
else
{
/* and now we count digits */
while(digit 0)
{
++digit[n % N];
digit /= N;
}
}

You now have your digit count, and it remains only to iterate through your
array, printing out the digits in whichever order it seems best to you,
making sure you print each the appropriate number of times.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


<rh*******@gmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...

Hi,

I''m trying to create a function that will sort a number''s digits
from highest to lowest.

For example

1000 - will become 0001

You mean 1000 will stay 1000?

or 1234 to 4321

Is it better to covert the number to a string first then sort? or
is there a better way to do it?
I just need some clues on how to do it. I''ve tried to search some
materials on how to do it but I only saw how to sort a ''LIST'' but not
just a number.
Thanks for the help

There is a number of solutions. If you don''t like to sort too much, and your
number is an integer, you could always do the following: If your number is in
base N, then make an array of integers which is N elements long (and initially
all elements are 0).

Now take your number and do a mod N on it. The result is the last digit of that
number. Let''s say the digit is X. Increase the X-th element of your array by 1.
Now divide the original number by N. Since your number and N are both integers,
what you will do is remove the last digit. Now do the same for the other digits
in a loop, until what is left of your number is 0.

Now you will have an array with its elements telling you how many of each digit
there is. Define Y as integer and set it to 1. Iterate through the array. If the
current element is 0, decrease it by 1 and do:

result += X * Y;
Y *= N;

Where X is the current element of the array (not its value, but its position in
that array). I assume ''result'' is equal to 0 before this. Do this until the X-th
element is = 0.
When these two loops are done, variable ''result'' will contain your result.

--
"It is easy in the world to live after the world''s oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/


这篇关于从最高到最低排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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