using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
namespace Training.ArraySort
{
class Program
{
static void Main(string[] args)
{
int[] IntArray = new int[10]; // 存放位排序后的整数
int IntNum = 0; // 存放合法输入的整数的个数
Boolean GoEnter = true; // 是否继续输入
while (GoEnter)
{
Console.WriteLine("每输入一个整数后回车,停止输入请输入Q:");
string strInt = Console.ReadLine();
if (IntNum > 9 || (strInt == "Q")) // 输入个数超过9个或输入Q时结束,防止越界
{
Console.WriteLine("输入结束!");
GoEnter = false;
}
else
{
if(IsInteger(strInt)) // 判断输入的是否是合法整数
{
char[] c = strInt.ToCharArray();
IntArray[IntNum++]=SortChar(c); // 整数各个位按升序排序
}
else
{
Console.WriteLine("请正确输入整数:"); //输入不合法
}
}
}
SortArray(IntArray); //升序排序后的各个整数比较大小(冒泡)
Console.WriteLine("排序后的数组是:");
#region 输出排序后的数组
for (int i = 0; i < IntArray.Length; i++)
{
if (IntArray[i] != 0)
{
Console.WriteLine(+IntArray[i]);
}
}
#endregion
Console.WriteLine("排序后数组中的最大数是:");
Console.WriteLine(IntArray[IntArray.Length - 1]) ;//排序后的最后一个就是最大值
Console.ReadKey();
}
#region IsInteger()判断是否为合法整数
private static bool IsInteger(string s)
{
string pattern = @"^\d*$";
return Regex.IsMatch(s, pattern);
}
#endregion
#region SortChar() 对输入的整数的各个位排序并去掉前面的0
private static int SortChar(char[] arr)
{
char[] end = new char[arr.Length];
int N = 0;
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
char temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)//去掉前面的0
{
if (arr[i]!='0')
{
end[N++] = arr[i];
}
}
String ch2str = new String(arr);
return (int.Parse(ch2str));
}
#endregion
#region SortArray()各个位按升序拍好后比较大小
private static int[]SortArray(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
return(arr);
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
namespace Training.ArraySort
{
class Program
{
static void Main(string[] args)
{
int[] IntArray = new int[10]; // 存放位排序后的整数
int IntNum = 0; // 存放合法输入的整数的个数
Boolean GoEnter = true; // 是否继续输入
while (GoEnter)
{
Console.WriteLine("每输入一个整数后回车,停止输入请输入Q:");
string strInt = Console.ReadLine();
if (IntNum > 9 || (strInt == "Q")) // 输入个数超过9个或输入Q时结束,防止越界
{
Console.WriteLine("输入结束!");
GoEnter = false;
}
else
{
if(IsInteger(strInt)) // 判断输入的是否是合法整数
{
char[] c = strInt.ToCharArray();
IntArray[IntNum++]=SortChar(c); // 整数各个位按升序排序
}
else
{
Console.WriteLine("请正确输入整数:"); //输入不合法
}
}
}
SortArray(IntArray); //升序排序后的各个整数比较大小(冒泡)
Console.WriteLine("排序后的数组是:");
#region 输出排序后的数组
for (int i = 0; i < IntArray.Length; i++)
{
if (IntArray[i] != 0)
{
Console.WriteLine(+IntArray[i]);
}
}
#endregion
Console.WriteLine("排序后数组中的最大数是:");
Console.WriteLine(IntArray[IntArray.Length - 1]) ;//排序后的最后一个就是最大值
Console.ReadKey();
}
#region IsInteger()判断是否为合法整数
private static bool IsInteger(string s)
{
string pattern = @"^\d*$";
return Regex.IsMatch(s, pattern);
}
#endregion
#region SortChar() 对输入的整数的各个位排序并去掉前面的0
private static int SortChar(char[] arr)
{
char[] end = new char[arr.Length];
int N = 0;
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
char temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)//去掉前面的0
{
if (arr[i]!='0')
{
end[N++] = arr[i];
}
}
String ch2str = new String(arr);
return (int.Parse(ch2str));
}
#endregion
#region SortArray()各个位按升序拍好后比较大小
private static int[]SortArray(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
return(arr);
}
#endregion
}
}