Last active
September 18, 2019 20:31
-
-
Save unilecs/7e4b42682ef4c9e30df95192740fc70a to your computer and use it in GitHub Desktop.
Напишите функцию, которая принимает список не отрицательных целых чисел, упорядочивает их так, чтобы они составляли максимально возможное число.
This file contains 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.Linq; | |
public class Program | |
{ | |
public class NumberDegree | |
{ | |
public int Origin { get; set; } | |
public double NewValue { get; set; } | |
} | |
public static int[] GetMaxNumberFromArray(int[] arr) | |
{ | |
var numberDegree = new NumberDegree[arr.Length]; | |
// Проходим по массиву и нНаходим максимальное кол-во цифр в числе | |
int maxCountDigits = 0; | |
for (var i = 0; i < arr.Length; i++) | |
{ | |
var countDigits = arr[i].ToString().Length; | |
if (maxCountDigits < countDigits) | |
{ | |
maxCountDigits = countDigits; | |
} | |
} | |
// формируем новый массив, дополняя каждое число первой цифрой до степени макисмального числа в массиве | |
// Например, [ 819, 6, 89, 47 ] -> [ 819, 666, 899, 477 ] | |
for (var j = 0; j < arr.Length; j++) | |
{ | |
var countRepeat = maxCountDigits - arr[j].ToString().Length; | |
var firstDigit = arr[j].ToString().FirstOrDefault(); | |
var newValue = arr[j].ToString() + string.Concat(Enumerable.Repeat(firstDigit, countRepeat)); | |
numberDegree[j] = new NumberDegree { Origin = arr[j], NewValue = Convert.ToDouble(newValue) }; | |
} | |
// Сортируем "новый" массив по убыванию | |
int[] maxNumberFromArr = numberDegree.OrderByDescending(x => x.NewValue).Select(x => x.Origin).ToArray(); | |
return maxNumberFromArr; | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
var arr = new int[] { 819, 6, 54, 5, 89, 4023, 402 }; | |
foreach(var number in GetMaxNumberFromArray(arr)) | |
{ | |
Console.Write(number + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
не работает на этом наборе {121, 12, 95}