如题
给一个整数数组,对数组中的每个整数中的所有数字按照升序排列(如101排序后为011)请写一个方法,输出排序后的数组中的最大数。
例如有一个数组: 101、132、375,排序后011、123、357,所以其中最大的数是357。
解
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
int[] Numbers = new int[10];
int Count = 0;
bool Flag = true;
Console.WriteLine("Plaease input 10 or less numbers");
while (Flag)
{
String i1 = Console.ReadLine();
//Console.WriteLine(i1);
if (Regex.IsMatch(i1, @"^\d+$"))
{
Numbers[Count] = Sort(int.Parse(i1));
Count++;
}
else
{
if(i1 != "")
{
Console.WriteLine("Illegal input");
}
Flag = false;
if(Count == 0)
{
return -1;
}
}
}
for (int i = 0; i < Numbers.Length; i++)
{
for (int j = 0; j < Numbers.Length - 1; j++)
{
if (Numbers[i] > Numbers[j])
{
int tmp = Numbers[i];
Numbers[i] = Numbers[j];
Numbers[j] = tmp;
}
}
}
Console.WriteLine(Numbers[--Count]);
return 0;
}
private static int Sort(int entry)
{
char[] result = new char[10];
int resultInt = 0;
String temp = entry.ToString();
result = temp.ToCharArray();
int length = entry.ToString().Length;
for(int i = 0; i<length; i++)
{
for(int j = 0; j<length-1; j++)
{
if(result[i] < result[j])
{
char tmp = result[i];
result[i] = result[j];
result[j] = tmp;
}
}
}
//Console.WriteLine(result);
for (int i = 0; i<result.Length; i++)
{
resultInt = resultInt * 10 + result[i]-'0';
}
//Console.WriteLine(resultInt);
return resultInt;
}
}
}