Looking for the next several full array

Foreword

topic:

It gives a positive integer, to find out the whole positive integer number so arranged that the next number.
For example, 12345. 12354 is returned.

12354 return 12435

Their answers

class Program
{
    public static int[] GetArrays(int num)
    {
        string len = num.ToString();
        int[] intarr = new int[len.Length];
        int index = 0;
        while (num != 0)
        {
           int yu=num % 10;
            intarr[index] = yu;
            index++;
            num = num / 10;
        }
        return intarr;
    }

    public static int getMaxInt(int[] intarr)
    {
        var sortIndex = 0;
        var temp = 0;
        for (int i = 1; i < intarr.Length; i++)
        {
            if (intarr[i] < intarr[0])
            {
                sortIndex = i - 1;
                temp = intarr[0];
                intarr[0] = intarr[i];
                intarr[i] = temp;
                break;
            }
        }

        var hasSore = true;
        if (sortIndex - 1 >= 0)
        {
            for (int i = 0; i < sortIndex ; i++)
            {
                for (int j = 0; j < sortIndex- i; j++)
                {
                    if (intarr[j] < intarr[j + 1])
                    {
                        temp = intarr[j + 1];
                        intarr[j + 1] = intarr[j];
                        intarr[j] = temp;
                        hasSore = false;
                    }
                    if (hasSore)
                    {
                        break;
                    }
                }
            }
        }
        int result = 0;
        for (int i = 0; i < intarr.Length; i++)
        {
            var tempdouble = Convert.ToDouble(i);
            result += Convert.ToInt32(intarr[i] * Math.Pow(10.0, tempdouble));
        }
        return result;
    }
    static void Main(string[] args)
    {
        Console.WriteLine(getMaxInt(GetArrays(12354)));
        Console.ReadKey();
    }
}

Guess you like

Origin www.cnblogs.com/aoximin/p/12533240.html