-
-
Save Suchiman/c78b8c7bf5aac0326472c848a13135c2 to your computer and use it in GitHub Desktop.
LexicographSort Integer List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace LexicographicSortIntegerList | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
while (true) | |
{ | |
// echo "41 4\n" | dotnet run (Output 441) | |
List<int> digits = "41 4" | |
.Split(' ') | |
.Select(Int32.Parse) | |
.ToList(); | |
digits.Sort((a, b) => | |
{ | |
int aMagnitude = (int)Math.Pow(10, (int)(Math.Log10(a) + 1)); | |
int bMagnitude = (int)Math.Pow(10, (int)(Math.Log10(b) + 1)); | |
int left = a * bMagnitude + b; | |
int right = b * aMagnitude + a; | |
return right.CompareTo(left); | |
}); | |
Console.WriteLine(String.Join("", digits)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment