Advertisement
SteamGear

Untitled

Jul 3rd, 2020
1,698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.58 KB | None | 0 0
  1.  static public void Problem24()
  2.         {
  3.             //A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4.If all of the permutations are listed numerically or alphabetically, we call it lexicographic order.The lexicographic permutations of 0, 1 and 2 are:
  4.             //012   021   102   120   201   210
  5.             //What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 ?
  6.  
  7.             bool[] isUsed = new bool[10];
  8.             int[] permutation = new int[10];
  9.  
  10.             StringBuilder sb = new StringBuilder();
  11.  
  12.             //(╥_╥)
  13.             int count = 0;
  14.             for (int a = 0; a < 10; a++)
  15.             {
  16.                 isUsed[a] = true;
  17.  
  18.                 for (int b = 0; b < 10; b++)
  19.                 {
  20.                     if (isUsed[b])
  21.                     {
  22.                         continue;
  23.                     }
  24.                     else
  25.                     {
  26.                         isUsed[b] = true;
  27.                         for (int c = 0; c < 10; c++)
  28.                         {
  29.                             if (isUsed[c])
  30.                             {
  31.                                 continue;
  32.                             }
  33.                             else
  34.                             {
  35.                                 isUsed[c] = true;
  36.                                 for (int d = 0; d < 10; d++)
  37.                                 {
  38.                                     if (isUsed[d])
  39.                                     {
  40.                                         continue;
  41.                                     }
  42.                                     else
  43.                                     {
  44.                                         isUsed[d] = true;
  45.                                         for (int e = 0; e < 10; e++)
  46.                                         {
  47.                                             if (isUsed[e])
  48.                                             {
  49.                                                 continue;
  50.                                             }
  51.                                             else
  52.                                             {
  53.                                                 isUsed[e] = true;
  54.                                                 for (int f = 0; f < 10; f++)
  55.                                                 {
  56.                                                     if (isUsed[f])
  57.                                                     {
  58.                                                         continue;
  59.                                                     }
  60.                                                     else
  61.                                                     {
  62.                                                         isUsed[f] = true;
  63.                                                         for (int g = 0; g < 10; g++)
  64.                                                         {
  65.                                                             if (isUsed[g])
  66.                                                             {
  67.                                                                 continue;
  68.                                                             }
  69.                                                             else
  70.                                                             {
  71.                                                                 isUsed[g] = true;
  72.                                                                 for (int h = 0; h < 10; h++)
  73.                                                                 {
  74.                                                                     if (isUsed[h])
  75.                                                                     {
  76.                                                                         continue;
  77.                                                                     }
  78.                                                                     else
  79.                                                                     {
  80.                                                                         isUsed[h] = true;
  81.                                                                         for (int i = 0; i < 10; i++)
  82.                                                                         {
  83.                                                                             if (isUsed[i])
  84.                                                                             {
  85.                                                                                 continue;
  86.                                                                             }
  87.                                                                             else
  88.                                                                             {
  89.                                                                                 isUsed[i] = true;
  90.                                                                                 for (int j = 0; j < 10; j++)
  91.                                                                                 {
  92.                                                                                     if (isUsed[j])
  93.                                                                                     {
  94.                                                                                         continue;
  95.                                                                                     }
  96.                                                                                     else
  97.                                                                                     {
  98.                                                                                         count++;
  99.                                                                                         isUsed[j] = true;
  100.  
  101.                                                                                         sb.Clear();
  102.                                                                                         if (count == 1000000)
  103.                                                                                         {
  104.                                                                                             permutation[0] = a;
  105.                                                                                             permutation[1] = b;
  106.                                                                                             permutation[2] = c;
  107.                                                                                             permutation[3] = d;
  108.                                                                                             permutation[4] = e;
  109.                                                                                             permutation[5] = f;
  110.                                                                                             permutation[6] = g;
  111.                                                                                             permutation[7] = h;
  112.                                                                                             permutation[8] = i;
  113.                                                                                             permutation[9] = j;
  114.  
  115.                                                                                             for (int r = 0; r < 10; r++)
  116.                                                                                             {
  117.                                                                                                 sb.Append(permutation[r]);
  118.                                                                                             }
  119.                                                                                             Console.Write(sb + "");
  120.                                                                                             goto end;
  121.                                                                                         }
  122.                                                                                     }
  123.                                                                                     isUsed[j] = false;
  124.                                                                                 }
  125.                                                                             }
  126.                                                                             isUsed[i] = false;
  127.                                                                         }
  128.                                                                     }
  129.                                                                     isUsed[h] = false;
  130.                                                                 }
  131.                                                             }
  132.                                                             isUsed[g] = false;
  133.                                                         }
  134.                                                     }
  135.                                                     isUsed[f] = false;
  136.                                                 }
  137.                                             }
  138.                                             isUsed[e] = false;
  139.                                         }
  140.                                     }
  141.                                     isUsed[d] = false;
  142.                                 }
  143.                             }
  144.                             isUsed[c] = false;
  145.                         }
  146.                     }
  147.                     isUsed[b] = false;
  148.                 }
  149.                 isUsed[a] = false;
  150.             }
  151.             end:;
  152.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement