如何对整数(int)中的数字进行排序 [英] how to sort number that in integer (int)

查看:161
本文介绍了如何对整数(int)中的数字进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用数组和字符串的情况下对整数(int)中的数字进行排序????

例如

how to sort number that in integer (int) without the use of array and string????
for example

int x=7324



排序(x)的数字是:2347


sort of (x) number is : 2347

推荐答案

到目前为止,我只提出了一些简单的解决方案:再使用一个中间 int 对象。问题的制定并不禁止这一点。您可以使用它来表示一种数字数组。您只有10位数字,因此数字对象只需要4位(半字节)来表示。但是,最大值,例如,32位 int 值只需10位小数,因此您甚至可以将整个字节用于表示一个十进制数字。



然后你需要的只是简单的位运算来提取,设置和移动具有这样的整数对象的字节,以及任何合适的排序算法。



我担心这不是最优雅的解决方案,但是在我第一眼看到这个问题之后,我就能立即创造出来。我已经告诉了你太多,所以我害怕在可能的不公平竞争中给你一个好处。所以,不是源代码。现在任何人都可以解决问题。无论如何,你甚至没有指定语言...



[免责声明]



当我写这篇文章时解决方案我对仅使用%和/运算符的条件一无所知。询问者稍后在评论中添加了它。



-SA
So far, I came up with only some trivial solution: use one more intermediate int object. This is not prohibited by the problems formulation. You can use it to represent a kind of "array of digits". You have only 10 digit, so a digit object needs only 4 bits (half-byte) to represent. But maximum, say, 32-bit int value takes only 10 decimal digits, so you can even dedicate whole byte to represent one decimal digit.

Then all you need is simple bit arithmetic to extract, set and move bytes withing such integer object, and any suitable sorting algorithm.

I'm afraid this is not the most elegant solution, but there is what I was able to create immediately after my first glance at the problem. I already told you too much so I'm afraid of giving you a benefit in possible unfair competition. So, not source code. Anyone can solve the problem now. Anyway, you did not even specify the language…

[DISCLAIMER]

When I was writing this solution I did not know anything about the condition of using only % and / operators. Inquirer added it in comment later.

—SA


这样做。它将处理多次出现的数字但当然不是0(当前0被忽略)。如果需要,则必须在9之后放置0.很容易做到。



This will do it. It will handle multiple occurrences of digits but not 0 of course (at present 0 is ignored). If required 0 would have to be placed after 9. Easy enough to do.

#include <stdio.h>

int main () {
	const __int64 input = 456120;
	__int64 output = 0, temp1;
	int len = 0;

	// Calculate the number of digits.
	temp1 = input;
	while(temp1 > 0)
	{
		temp1/=10;
		len++;
	}

	// Work through the digits 0-9.
	for (int i=0; i <= 9; i++)
	{
		// Will catch multiple occurences.
		for (temp1 = input; temp1 > 0; temp1 /= 10)
		{
			if (temp1%10 == i)
			{
				// Put the digit in its correct position in output integer.
				__int64 temp2 = i;
				--len;

				for(int count = len; count > 0; count--)
					temp2*=10;

				output += temp2;
			}
		}
	}

	printf("%d\n", output);
}


初步说明:请阅读我对该问题的评论。您没有提供足够的信息。后来的补充和修改被忽略了。



不,不可能改变数字内的数字顺序!因此,从 7324 2347 的转换不使用字符数组(字符串)或整数是不可能的! - 由于谢尔盖的评论,我必须通过这部分答案。是的,从理论上讲,可以按升序移动数字 - 参见解决方案1.



最好的方法是转换 7324 value to string(char array)或整数数组然后循环遍历字符/整数数组并更改排序顺序。

请参阅:

http://www.csharp-examples.net/sort-array/ [ ^ ]

C#中排序算法的可视化和比较 [ ^ ]

在C#.NET中排序算法代码 [ ^ ]



另一个方法是使用 Array.Sort [ ^ ]方法。



或者......使用Linq:

Preliminary note: Please, read my comment to the question. You did not provide enough information. Later additions and amendments have been ignored.

No, it's impossible to "change the order of digits inside the number"! So, "conversion" from 7324 to 2347 whithout using array of chars (string) or integers is not possible! - I have to "strike through" this part of my answer, because of Sergey's comments. Yes, theoretically it's possible to moves digits in ascending order - see Solution 1.

The best way is to convert 7324 value to string (char array) or integer array then loop through the array of chars/integers and change the sort order.
See:
http://www.csharp-examples.net/sort-array/[^]
Visualization and comparison of sorting algorithms in C#[^]
Sorting Algorithms Codes in C#.NET[^]

Another way is to use Array.Sort[^] method.

Or... using Linq:
int x=7324;
var sortOfX = x.ToString().OrderBy(c=>Convert.ToInt32(c)).Select(a=>a.ToString()).Aggregate((a,b) => string.Concat(a, b));
//returns: 2347


这篇关于如何对整数(int)中的数字进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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