我试图按升序排序任何长度的整数的数字,而不使用字符串,数组或递归。
例:
Input: 451467
Output: 144567
我已经弄清楚如何用模数除法得到整数的每个数字:
int number = 4214;
while (number > 0) {
IO.println(number % 10);
number = number / 10;
}
但我不知道如何在没有数组的情况下订购数字。
不要担心IO
类;这是我们教授给我们的定制课程。
实际上有一个非常简单的算法,它只使用整数:
int number = 4214173;
int sorted = 0;
int digits = 10;
int sortedDigits = 1;
boolean first = true;
while (number > 0) {
int digit = number % 10;
if (!first) {
int tmp = sorted;
int toDivide = 1;
for (int i = 0; i < sortedDigits; i++) {
int tmpDigit = tmp % 10;
if (digit >= tmpDigit) {
sorted = sorted/toDivide*toDivide*10 + digit*toDivide + sorted % toDivide;
break;
} else if (i == sortedDigits-1) {
sorted = digit * digits + sorted;
}
tmp /= 10;
toDivide *= 10;
}
digits *= 10;
sortedDigits += 1;
} else {
sorted = digit;
}
first = false;
number = number / 10;
}
System.out.println(sorted);
它将打印出1123447
。这个想法很简单:
该版本的算法可以在asc和desc命令中进行排序,您只需要更改条件即可。
另外,我建议你看看所谓的Radix Sort,解决方案here从基数排序中获取一些想法,我认为基数排序是该解决方案的一般情况。
这是4行,基于你的while循环的for
循环变体,带有一点java 8 spice:
int number = 4214;
List<Integer> numbers = new LinkedList<>(); // a LinkedList is not backed by an array
for (int i = number; i > 0; i /= 10)
numbers.add(i % 10);
numbers.stream().sorted().forEach(System.out::println); // or for you forEach(IO::println)
我假设你被允许使用哈希。
public static void sortDigits(int x) {
Map<Integer, Integer> digitCounts = new HashMap<>();
while (x > 0) {
int digit = x % 10;
Integer currentCount = digitCounts.get(digit);
if (currentCount == null) {
currentCount = 0;
}
digitCounts.put(x % 10, currentCount + 1);
x = x / 10;
}
for (int i = 0; i < 10; i++) {
Integer count = digitCounts.get(i);
if (count == null) {
continue;
}
for (int j = 0; j < digitCounts.get(i); j++) {
System.out.print(i);
}
}
}
如何在不使用数组,字符串或排序API的情况下对数字进行排序?好吧,您可以按照以下简单步骤对数字进行排序(如果读取的内容太多,请参阅下面的调试输出以了解排序是如何完成的):
我在main方法和一个函数中提供了两个while循环的代码。该函数什么都不做,但是,构建一个新的整数,不包括传递给的数字,例如我传递函数451567和1,函数返回45567(以任何顺序,无关紧要)。如果此函数通过了451567和5,则它会在数字中找到5位数并将它们添加到存储并返回没有5位数的数字(这样可以避免额外处理)。
调试,知道它如何对整数进行排序:
最后一位是:7的数字:451567 子块是45156 子块是4515 子块是451 子块是45 Subchunk是4 451567中的小数位是1 店铺是:1 从451567中删除1 减少的数量是:76554 最后一位是:4的数字:76554 子块是7655 Subchunk是765 Subchunk是76 Subchunk是7 76554中的数字为4 店铺是:14 从76554删除4 减少的数量是:5567 最后一位是:7的数字:5567 Subchunk是556 Subchunk是55 子块是5 5567的数字是5 店铺是:145 从5567中删除5 找到重复的最小数字5。店铺是:145 重复的最小数字5添加到商店。更新的商店是:1455 减少的数量是:76 最后一位是:数字6:76 Subchunk是7 76中的数字是6 店铺是:14556 从76中删除6 减少的数量是:7 最后一位是:7的数字:7 7中的数字是7 商店是:145567 从7中删除7 减少的数量是:0 升序451567是145567
示例代码如下:
//stores our sorted number
static int store = 0;
public static void main(String []args){
int number = 451567;
int original = number;
while (number > 0) {
//digit by digit - get last most digit
int digit = number % 10;
System.out.println("Last digit is : " + digit + " of number : " + number);
//get the whole number minus the last most digit
int temp = number / 10;
//loop through number minus the last digit to compare
while(temp > 0) {
System.out.println("Subchunk is " + temp);
//get the last digit of this sub-number
int t = temp % 10;
//compare and find the lowest
//for sorting descending change condition to t > digit
if(t < digit)
digit = t;
//divide the number and keep loop until the smallest is found
temp = temp / 10;
}
System.out.println("Smalled digit in " + number + " is " + digit);
//add the smallest digit to store
store = (store * 10) + digit;
System.out.println("Store is : " + store);
//we found the smallest digit, we will remove that from number and find the
//next smallest digit and keep doing this until we find all the smallest
//digit in sub chunks of number, and keep adding the smallest digits to
//store
number = getReducedNumber(number, digit);
}
System.out.println("Ascending order of " + original + " is " + store);
}
/*
* A simple method that constructs a new number, excluding the digit that was found
* to b e smallest and added to the store. The new number gets returned so that
* smallest digit in the returned new number be found.
*/
public static int getReducedNumber(int number, int digit) {
System.out.println("Remove " + digit + " from " + number);
int newNumber = 0;
//flag to make sure we do not exclude repeated digits, in case there is 44
boolean repeatFlag = false;
while(number > 0) {
int t = number % 10;
//assume in loop one we found 1 as smallest, then we will not add one to the new number at all
if(t != digit) {
newNumber = (newNumber * 10) + t;
} else if(t == digit) {
if(repeatFlag) {
System.out.println("Repeated min digit " + t + "found. Store is : " + store);
store = (store * 10) + t;
System.out.println("Repeated min digit " + t + "added to store. Updated store is : " + store);
//we found another value that is equal to digit, add it straight to store, it is
//guaranteed to be minimum
} else {
//skip the digit because its added to the store, in main method, set flag so
// if there is repeated digit then this method add them directly to store
repeatFlag = true;
}
}
number /= 10;
}
System.out.println("Reduced number is : " + newNumber);
return newNumber;
}
}
我的算法:
int ascending(int a)
{
int b = a;
int i = 1;
int length = (int)Math.log10(a) + 1; // getting the number of digits
for (int j = 0; j < length - 1; j++)
{
b = a;
i = 1;
while (b > 9)
{
int s = b % 10; // getting the last digit
int r = (b % 100) / 10; // getting the second last digit
if (s < r)
{
a = a + s * i * 10 - s * i - r * i * 10 + r * i; // switching the digits
}
b = a;
i = i * 10;
b = b / i; // removing the last digit from the number
}
}
return a;
}
添加一个非常简单的算法,不需要任何数据结构或花哨的数学,就像其他人那样。
int number = 65356;
for (int i = 0; i <= 9; i++) { // the possible elements are known, 0 to 9
int tempNumber = number;
while (tempNumber > 0) {
int digit = tempNumber % 10;
IO.print(digit);
tempNumber = number / 10;
}
}
用语言: 1.在数字中每0打印一个0。 2.在数字中每1打印1。 ...
这是一个简单的解决方案:
public class SortDigits
{
public static void main(String[] args)
{
sortDigits(3413657);
}
public static void sortDigits(int num)
{
System.out.println("Number : " + num);
String number = Integer.toString(num);
int len = number.length(); // get length of the number
int[] digits = new int[len];
int i = 0;
while (num != 0)
{
int digit = num % 10;
digits[i++] = digit; // get all the digits
num = num / 10;
}
System.out.println("Digit before sorting: ");
for (int j : digits)
{
System.out.print(j + ",");
}
sort(digits);
System.out.println("\nDigit After sorting: ");
for (int j : digits)
{
System.out.print(j + ",");
}
}
//simple bubble sort
public static void sort(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
for (int j = i + 1; j < arr.length; j++)
{
if (arr[i] > arr[j])
{
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
}
class SortDigits {
public static void main(String[] args) {
int inp=57437821;
int len=Integer.toString(inp).length();
int[] arr=new int[len];
for(int i=0;i<len;i++)
{
arr[i]=inp%10;
inp=inp/10;
}
Arrays.sort(arr);
int num=0;
for(int i=0;i<len;i++)
{
num=(num*10)+arr[i];
}
System.out.println(num);
}
}