(1) 定义一个元素个数不确定的数组,对数组进行排序,并求数组中的最大值和最小值.求C#代码

2024-12-26 12:26:17
推荐回答(2个)
回答1:

static void Main(string[] args)
{
Console.WriteLine("请输入你要排序的数,中间用\",\"分隔");
string input = Console.ReadLine();//接收输入的字符串
string[] num = input.Split(',');//字符串通过','分割
double[] shuzu = new double[num.Length]; //声明一维数组
for (int j = 0; j < num.Length; j++)//给一维数组赋值
{
shuzu[j] = Convert.ToDouble(num[j]);
}
//开始冒泡排序
for (int x = 0; x < shuzu.Length-1; x++)
{
for (int y = 0; y < shuzu.Length-1-x; y++)
{
if (shuzu[y] > shuzu[y + 1]) //用两个变量的方式交换大小
{
shuzu[y] = shuzu[y] + shuzu[y + 1];
shuzu[y + 1] = shuzu[y] - shuzu[y + 1];
shuzu[y] = shuzu[y] - shuzu[y + 1];
}
}
}
Console.WriteLine("你输入的数进行升序排序后输出为:");
//循环把排序后的数组打印出来
for (int i = 0; i {
Console.WriteLine(shuzu[i]);
}
//让窗口暂停
Console.ReadLine();
}

回答2:

C行不?