排序的整数的位数 [英] Sorting digits of an integer

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

问题描述

您将得到一个整数 51234 (说),我们需要一个数字输出将是数字排序 12345

You are given an integer 51234 (say) we need to sort the digits of a number the output will be 12345.

怎么做,而不使用数组吗?

How to do it without using array ?

推荐答案

您可以使用一个循环和 10%来提取每个数字。
从0到9的外环可以用来测试是否位存在。如果它存在,打印出来。

You can use a loop and % 10 to extract each digit. An outer loop from 0 to 9 could be used to test if the digit exists. If it exists, print it.

在伪code:

n = integer // 51234
FOR digit = 0 TO 9
  temp = n
  REPEAT
    IF temp % 10 = digit THEN PRINT digit
    temp /= 10
  UNTIL temp = 0

编辑::该测试在海湾合作委员会表明,它处理零和重复数字:

This test in gcc shows that it handles zeros and repeated digits:

$ cat sortdigits.c
#include <stdio.h>
main () {
 int n,digit,temp;
 n = 43042025;
 for (digit=0;digit<9;digit++)
   for (temp=n;temp>0;temp/=10)
     if (temp%10==digit) printf("%d",digit);
 printf("\n");
}
$ ./sortdigits
00223445

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

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