Advertisement
yahorrr

Untitled

Apr 12th, 2022
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. using System;
  2.  
  3. namespace NextBiggerTask
  4. {
  5.     public static class NumberExtension
  6.     {
  7.         /// <summary>
  8.         /// Finds the nearest largest integer consisting of the digits of the given positive integer number and null if no such number exists.
  9.         /// </summary>
  10.         /// <param name="number">Source number.</param>
  11.         /// <returns>
  12.         /// The nearest largest integer consisting of the digits  of the given positive integer and null if no such number exists.
  13.         /// </returns>
  14.         /// <exception cref="ArgumentException">Thrown when source number is less than 0.</exception>
  15.         public static int? NextBiggerThan(int number)
  16.         {
  17.             int n = number;
  18.             int length = NumberOfDigit(number);
  19.             var numberArray = new int[length - 1];
  20.  
  21.             for (int i = length; i > 0; i--)
  22.             {
  23.                 numberArray[i] = n % 10;
  24.                 n /= 10;
  25.             }
  26.  
  27.             int position = -1;
  28.  
  29.             for (int i = length; i > 0; i--)
  30.             {
  31.                 if (numberArray[i] > numberArray[i - 1])
  32.                 {
  33.                     position = i;
  34.                     break;
  35.                 }
  36.             }
  37.  
  38.             if (position == -1)
  39.             {
  40.                 return 0;
  41.             }
  42.  
  43.             var endOfNumberArray = numberArray[position..length];
  44.  
  45.             if (endOfNumberArray == BubbleSortLess(endOfNumberArray))
  46.             {
  47.                 position--;
  48.                 endOfNumberArray = numberArray[position..length];
  49.             }
  50.  
  51.             for (int i = 2; i < endOfNumberArray.Length; i++)
  52.             {
  53.                 int firstGreat = endOfNumberArray[1];
  54.                 if (endOfNumberArray[i] > endOfNumberArray[0] && endOfNumberArray[i] < firstGreat)
  55.                 {
  56.                     int temp = endOfNumberArray[0];
  57.                     endOfNumberArray[0] = endOfNumberArray[i];
  58.                     endOfNumberArray[i] = temp;
  59.                 }
  60.             }
  61.  
  62.             var result = new int[length - 1];
  63.             for (int i = 0; i < position; i++)
  64.             {
  65.                 result[i] = numberArray[i];
  66.             }
  67.  
  68.             result[position] = endOfNumberArray[0];
  69.  
  70.             var foo = new int[length - 1 - 1 - position];
  71.  
  72.             foo = BubbleSortGreat(foo);
  73.  
  74.             Array.Copy(endOfNumberArray, 1, foo, 0, length - 1 - 1 - position);
  75.  
  76.             for (int i = position + 1; i < length; i++)
  77.             {
  78.                 result[i] = foo[i];
  79.             }
  80.  
  81.             return ArrayToInt(result);
  82.         }
  83.  
  84.         private static int NumberOfDigit(int number)
  85.         {
  86.             int i = 0;
  87.  
  88.             while (number > 0)
  89.             {
  90.                 number /= 10;
  91.                 i++;
  92.             }
  93.  
  94.             return i;
  95.         }
  96.  
  97.         private static int ArrayToInt(int[] arr)
  98.         {
  99.             int number = 0;
  100.             for (int i = 0; i < arr.Length; i++)
  101.             {
  102.                 number *= 10;
  103.                 number += arr[i + 1];
  104.             }
  105.  
  106.             return number;
  107.         }
  108.  
  109.         private static int[] BubbleSortLess(int[] arr)
  110.         {
  111.             int n = arr.Length;
  112.             for (int i = 0; i < n - 1; i++)
  113.             {
  114.                 for (int j = 0; j < n - i - 1; j++)
  115.                 {
  116.                     if (arr[j] < arr[j + 1])
  117.                     {
  118.                         int temp = arr[j];
  119.                         arr[j] = arr[j + 1];
  120.                         arr[j + 1] = temp;
  121.                     }
  122.                 }
  123.             }
  124.  
  125.             return arr;
  126.         }
  127.  
  128.         private static int[] BubbleSortGreat(int[] arr)
  129.         {
  130.             int n = arr.Length;
  131.             for (int i = 0; i < n - 1; i++)
  132.             {
  133.                 for (int j = 0; j < n - i - 1; j++)
  134.                 {
  135.                     if (arr[j] > arr[j + 1])
  136.                     {
  137.                         int temp = arr[j];
  138.                         arr[j] = arr[j + 1];
  139.                         arr[j + 1] = temp;
  140.                     }
  141.                 }
  142.             }
  143.  
  144.             return arr;
  145.         }
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement