题解 | #数串#

数串

https://www.nowcoder.com/practice/a6a656249f404eb498d16b2f8eaa2c60

public class Program {
    public static void Main() {
        string inPut;
        string result = "";

        while ((inPut = System.Console.ReadLine()) != null) {
            string[] num = System.Console.ReadLine().Split(" ");

            //对字符串进行冒泡排序,规则是B+A大于A+B,则认为B>A,将B与A交换位置
            for (int i = 0; i < num.Length - 1; i++) {
                //如果某一轮没有发生比较 说明这个字符串数组已经有序了
                bool outflag = false;
                for (int j = 0; j < num.Length - 1 - i; j++) {
                    //如果j+1比j大,则交换位置
                    string str1 = num[j + 1] + num[j];
                    string str2 = num[j] + num[j + 1];
                    if (string.Compare(str1, str2) == 1) {
                        string tmp = num[j];
                        num[j] = num[j + 1];
                        num[j + 1] = tmp;
                        outflag = true;
                    }
                }
                if (!outflag)
                    break;
            }

            //记录输出结果
            for (int j = 0; j < num.Length; j++)
                result += num[j];

            //换行
            result += "\n";
        }
        System.Console.WriteLine(result);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务