Sorting digits of an integer

前端 未结 9 1279
日久生厌
日久生厌 2020-12-16 01:26

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 ?

9条回答
  •  我在风中等你
    2020-12-16 02:09

    Sure arrays are out, but we've got a better container anyway:

    void foo(unsigned i) {
      std::set digits;
      do {
        digits.insert(`0` + i % 10);
        i /= 10;
      while(i!=0);
    }
    

    Use multiset if your input includes numbers like 887 that should be printed as 788

提交回复
热议问题