List<int> list = new List<int>(); list.Add(1); list.Add(24); list.Add(11); list.Sort(); Console.WriteLine("从小到大:"); foreach (int i in list) Console.WriteLine(i); list.Reverse(); Console.WriteLine("从大到小:"); foreach (int i in list) Console.WriteLine(i); list.Add(10); list.Sort(); Console.WriteLine("从小到大:"); foreach (int i in list) Console.WriteLine(i); list.Reverse(); Console.WriteLine("从大到小:"); foreach (int i in list) Console.WriteLine(i);
static void Main(string[] args) { Func2(); //Func1(); } static int ComparisonGreater(int x, int y) { return ((x > y) ? -1 : ((x == y) ? 0 : 1)); } static void WriteList(List<int> list) { foreach (int num in list) { System.Console.Write(num + " "); } System.Console.WriteLine(); } static void Func2() { Random rand = new Random(); List<int> list = new List<int>(); List<int> list2 = new List<int>(); for (int i = 0; i < 10; i++) { int nTmp = rand.Next(100); list.Add(nTmp); list2.Add(nTmp); } System.Console.WriteLine("Orig String:"); WriteList(list); list.Sort(); System.Console.WriteLine("Increase String:"); WriteList(list); list2.Sort(ComparisonGreater); System.Console.WriteLine("Decrease String:"); WriteList(list2); int numNew = rand.Next(100); System.Console.WriteLine("New number is: " + numNew); int nIndex = 0; for (; nIndex<list.Count; nIndex++) { if (numNew < list[nIndex]) { list.Insert(nIndex, numNew); break; } } if (nIndex == list.Count) { list.Add(numNew); } System.Console.WriteLine("Increase String After Inster:"); WriteList(list); nIndex = 0; for (; nIndex < list2.Count; nIndex++) { if (numNew > list2[nIndex]) { list2.Insert(nIndex, numNew); break; } } if (nIndex == list2.Count) { list2.Add(numNew); } System.Console.WriteLine("Decrease String After Inster:"); WriteList(list2); }
public class NumberChoose { public static void Main() { InputChoose(); } public static void InputChoose() { string stringNumber = Console.ReadLine(); //如果没有输入多个数字则表示插入 if (stringNumber.IndexOf(",") < 0) { InsertNumber(stringNumber); } else { InputNumber(stringNumber); } } private static string[] result = null; //排序 public static void InputNumber(string stringNumber) { //分割数字 string[] NumberList = stringNumber.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); //排序 string temp = ""; for (int i = 0; i < NumberList.Length; i++) { for (int j = 0; j <= i; j++) { if (int.Parse(NumberList[i]) < int.Parse(NumberList[j])) { temp = NumberList[i]; NumberList[i] = NumberList[j]; NumberList[j] = temp; } } } result = NumberList; Output(); } //输出 public static void Output() { //显示顺序排序结果 for (int i = 0; i < result.Length; i++) { Console.WriteLine(result[i]); } //显示倒序排序结果 for (int i = result.Length - 1; i >= 0; i--) { Console.WriteLine(result[i]); } InputChoose(); } //插入 public static void InsertNumber(string numberString) { string[] newResult = new String[result.Length + 1]; int j = 0; for (int i = 0; i < result.Length; i++) { if (int.Parse(result[i]) > int.Parse(numberString) && j == 0) { newResult[i] = numberString; newResult[i + 1] = result[i]; } else if (j == 1) { newResult[i + j] = result[i]; } else { newResult[i] = result[i]; } } result = newResult; Output(); } }以,分割的 1、输入1,4,8,6 这样的数据表示原始数组,如果输入不包含,的则表示插入的 2、如果第二次输入,分割的则重新排序
//插入 public static void InsertNumber(string numberString) { string[] newResult = new String[result.Length + 1]; int j = 0; for (int i = 0; i < result.Length; i++) { if (int.Parse(result[i]) > int.Parse(numberString) && j == 0) { j=1; newResult[i] = numberString; newResult[i + 1] = result[i]; } else if (j == 1) { newResult[i + j] = result[i]; } else { newResult[i] = result[i]; } } result = newResult; Output(); }无语的错误
List<int> list = new List<int>(); list.Add(1);
list.Add(24);
list.Add(11); list.Sort();
Console.WriteLine("从小到大:");
foreach (int i in list)
Console.WriteLine(i); list.Reverse();
Console.WriteLine("从大到小:");
foreach (int i in list)
Console.WriteLine(i); list.Add(10); list.Sort();
Console.WriteLine("从小到大:");
foreach (int i in list)
Console.WriteLine(i); list.Reverse();
Console.WriteLine("从大到小:");
foreach (int i in list)
Console.WriteLine(i);
static void Main(string[] args)
{
Func2();
//Func1();
} static int ComparisonGreater(int x, int y)
{
return ((x > y) ? -1 : ((x == y) ? 0 : 1));
} static void WriteList(List<int> list)
{
foreach (int num in list)
{
System.Console.Write(num + " ");
}
System.Console.WriteLine();
}
static void Func2()
{
Random rand = new Random();
List<int> list = new List<int>();
List<int> list2 = new List<int>(); for (int i = 0; i < 10; i++)
{
int nTmp = rand.Next(100);
list.Add(nTmp);
list2.Add(nTmp);
} System.Console.WriteLine("Orig String:");
WriteList(list); list.Sort();
System.Console.WriteLine("Increase String:");
WriteList(list); list2.Sort(ComparisonGreater);
System.Console.WriteLine("Decrease String:");
WriteList(list2); int numNew = rand.Next(100);
System.Console.WriteLine("New number is: " + numNew); int nIndex = 0;
for (; nIndex<list.Count; nIndex++)
{
if (numNew < list[nIndex])
{
list.Insert(nIndex, numNew);
break;
}
}
if (nIndex == list.Count)
{
list.Add(numNew);
}
System.Console.WriteLine("Increase String After Inster:");
WriteList(list);
nIndex = 0;
for (; nIndex < list2.Count; nIndex++)
{
if (numNew > list2[nIndex])
{
list2.Insert(nIndex, numNew);
break;
}
}
if (nIndex == list2.Count)
{
list2.Add(numNew);
}
System.Console.WriteLine("Decrease String After Inster:");
WriteList(list2);
}
public class NumberChoose
{
public static void Main()
{
InputChoose();
} public static void InputChoose()
{
string stringNumber = Console.ReadLine();
//如果没有输入多个数字则表示插入
if (stringNumber.IndexOf(",") < 0)
{
InsertNumber(stringNumber);
}
else
{
InputNumber(stringNumber);
}
} private static string[] result = null;
//排序
public static void InputNumber(string stringNumber)
{
//分割数字
string[] NumberList = stringNumber.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
//排序
string temp = "";
for (int i = 0; i < NumberList.Length; i++)
{
for (int j = 0; j <= i; j++)
{
if (int.Parse(NumberList[i]) < int.Parse(NumberList[j]))
{
temp = NumberList[i];
NumberList[i] = NumberList[j];
NumberList[j] = temp;
}
}
}
result = NumberList;
Output();
}
//输出
public static void Output()
{
//显示顺序排序结果
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}
//显示倒序排序结果
for (int i = result.Length - 1; i >= 0; i--)
{
Console.WriteLine(result[i]);
}
InputChoose();
}
//插入
public static void InsertNumber(string numberString)
{
string[] newResult = new String[result.Length + 1];
int j = 0;
for (int i = 0; i < result.Length; i++)
{
if (int.Parse(result[i]) > int.Parse(numberString) && j == 0)
{
newResult[i] = numberString;
newResult[i + 1] = result[i];
}
else if (j == 1)
{
newResult[i + j] = result[i];
}
else
{
newResult[i] = result[i];
}
}
result = newResult;
Output();
}
}以,分割的
1、输入1,4,8,6 这样的数据表示原始数组,如果输入不包含,的则表示插入的
2、如果第二次输入,分割的则重新排序
//插入
public static void InsertNumber(string numberString)
{
string[] newResult = new String[result.Length + 1];
int j = 0;
for (int i = 0; i < result.Length; i++)
{
if (int.Parse(result[i]) > int.Parse(numberString) && j == 0)
{
j=1;
newResult[i] = numberString;
newResult[i + 1] = result[i];
}
else if (j == 1)
{
newResult[i + j] = result[i];
}
else
{
newResult[i] = result[i];
}
}
result = newResult;
Output();
}无语的错误
{
List<int> result = new List<int>();
for (int j = value.ToString().Length; j>0; j--)
{
int itemp = Convert.ToInt32(Decimal.Remainder(value, (Decimal)Math.Pow(10, j)) / (Decimal)Math.Pow(10, (j - 1)));
result.Add(itemp);
}
result.ToList<int>().OrderBy(t => t).ToList<int>().ForEach(t => Console.Write(t));//小到大
Console.WriteLine();
result.ToList<int>().OrderByDescending(t => t).ToList<int>().ForEach(t => Console.Write(t));//大到小
Console.WriteLine();
result.Add(8888); result.ToList<int>().OrderBy(t => t).ToList<int>().ForEach(t => Console.Write(t));//小到大
Console.WriteLine();
result.ToList<int>().OrderByDescending(t => t).ToList<int>().ForEach(t => Console.Write(t));//大到小
Console.WriteLine(); Console.ReadKey(); return result;
}调用
Console.Write("输入数字:");
string temp= Console.ReadLine();
Power3(Convert .ToDecimal (temp));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConAppOrderBy
{
class Program
{
static void Main(string[] args)
{
OrderByAsc("1,4,3,7,5"); OrderByDesc("1,4,3,7,5"); string insertStr; //需要插入的数字
int site; //插入的位置 Console.WriteLine("请输入一个插入的数字:");
insertStr = Console.ReadLine(); Console.WriteLine("请输入插入的位置:");
site = int.Parse(Console.ReadLine()); GetNewStr("1,4,3,7,5", insertStr, site); Console.ReadKey();
} //从大到小
public static void OrderByDesc(string str)
{
int temp = 0;
string[] strSplit = str.Split(',');
Console.WriteLine("从大到小排序:");
for (int i = 0; i < strSplit.Length; i++)
{
for (int j = i + 1; j < strSplit.Length; j++)
{
if (int.Parse(strSplit[i]) < int.Parse(strSplit[j]))
{
temp = int.Parse(strSplit[j]);
strSplit[j] = strSplit[i];
strSplit[i] = temp.ToString();
}
}
} Show(strSplit); } //从小到大
public static void OrderByAsc(string str)
{
int temp = 0;
string[] strSplit = str.Split(',');
Console.WriteLine("从小到大排序:");
for (int i = 0; i < strSplit.Length; i++)
{
for (int j = i + 1; j < strSplit.Length; j++)
{
if (int.Parse(strSplit[i]) > int.Parse(strSplit[j]))
{
temp = int.Parse(strSplit[j]);
strSplit[j] = strSplit[i];
strSplit[i] = temp.ToString();
}
}
} Show(strSplit);
} public static void Show(string[] str)
{
foreach (string s in str)
{
Console.WriteLine("{0}", s);
}
} /// <summary>
///
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="insertStr">插入的字符</param>
/// <param name="site">插入的位置</param>
public static void GetNewStr(string str, string chr, int site)
{
int j=0;
string newStr = null;
string[] strSplit = str.Split(',');
string[] newStrSplit = new string[str.Split(',').Length + 1];
strSplit.CopyTo(newStrSplit, 0); for (int i = site; i < newStrSplit.Length; i++)
{
newStrSplit[site + 1] = newStrSplit[site];
}
newStrSplit[site] = chr; foreach (string s in newStrSplit)
{
if (j == newStrSplit.Length-1) newStr += s;
else newStr += s + ",";
j += 1;
} Console.WriteLine("生成的新字符串为:{0}",newStr); Console.WriteLine("***********************生成的新字符串的排序***********************"); OrderByAsc(newStr); OrderByDesc(newStr);
}
}
}