输入一个以回车结束的字符串,将字符串中的字符按ASCII码从小到大顺序重组后输出。

2025-02-25 17:11:13
推荐回答(3个)
回答1:

上面人说用冒泡排序,我就写了个小程序,你看行不行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public static void swap(ref T a, ref T b)
{
T c;
c = a;
a = b;
b = c;
}
static void Main(string[] args)
{
//冒泡排序:
Console.WriteLine("请输入一个字符串");
char[] charArray = Console.ReadLine().ToCharArray();

Console.WriteLine();
Console.WriteLine("排序后:");

for (int i = 0; i < charArray.Length - 1; i++)
{
for (int j = i + 1; j < charArray.Length; j++)
{
if (charArray[i] > charArray[j])
{
swap(ref charArray[i], ref charArray[j]);
}
}
}
string str = new string(charArray);
Console.WriteLine(str); //显示结果
}
}
}

回答2:

先输入到数组中,然后冒泡排序,最后输出,这个很难吗!?

回答3:

Student