首页 > 试题广场 >

字符串排序(1)

[编程题]字符串排序(1)
  • 热度指数:42195 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question



输入描述:
输入有两行,第一行n

第二行是n个字符串,字符串之间用空格隔开


输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格
示例1

输入

5
c d a bb e

输出

a bb c d e
推荐
点击链接查看正确的代码https://ac.nowcoder.com/acm/contest/5657#question
编辑于 2020-12-29 17:33:39 回复(0)
let n = parseInt(readline())
let arr = readline().split(' ')
print(arr.sort().join(' '))

发表于 2022-08-18 00:06:39 回复(0)
import sys
# 读入第一行
num = int(input())
# 定义一个空字符串
Str = ""
# 读入第二行
for line in sys.stdin:
    List = list(map(str, line.split()))
    # 使用sort()方法对字符串进行排序
    List.sort()
    for i in range(len(List)):
        shortStr = str(List[i])
        Str += shortStr
        if i < num - 1:
            Str += ' '
    print(Str)

发表于 2022-01-15 10:15:19 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()) {
            int num = Integer.parseInt(in.nextLine());
            String[] str = new String[num];
            str = in.nextLine().split(" ");

            Arrays.sort(str);
            
            for(int i = 0; i < num; i++) {
                System.out.print(str[i] + " ");
            }
            
        }
    }
}

发表于 2021-08-20 10:37:35 回复(0)
#include <iostream>
#include <string>
using namespace std;
 
intmain() {
    inta;
    cin >> a;
    string* b = newstring[a];
    int* c = newint[a];
    for(inti = 0;i < a;i++){
        cin >> b[i];
        c[i] = int(b[i][0]);
    }
    for(inti = 0;i < a;i++){
        intl = 999, m = 0;
        for(intj = 0;j < a;j++){
            if(c[j] < l){
                l = c[j];
                m = j;
            }
        }
        cout << b[m] << " ";
        c[m] = 999;
    }
     
}
编辑于 2024-03-01 00:46:57 回复(0)
input()
print(" ".join(sorted(input().split())))
发表于 2023-09-13 20:17:26 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// 比较函数,用于排序
int compareStrings(const void* a, const void* b) {
    return strcmp(*(const char**)a, *(const char**)b);
}

int main() {
    int n;
    scanf("%d", &n);

    // 为字符串数组分配内存
    char** strings = (char**)malloc(n * sizeof(char*));

    // 读取字符串并存储在数组中
    for (int i = 0; i < n; i++) {
        char buffer[100];  // 假设每个字符串最大长度为100
        scanf("%s", buffer);
        strings[i] = strdup(buffer); // 复制字符串并存储
    }

    // 使用快速排序对字符串数组进行排序
    qsort(strings, n, sizeof(char*), compareStrings);

    // 输出排序后的字符串,空格隔开
    for (int i = 0; i < n; i++) {
        printf("%s", strings[i]);
        if (i < n - 1) {
            printf(" "); // 添加空格,除了最后一个字符串
        }
        free(strings[i]); // 释放字符串内存
    }

    free(strings); // 释放字符串数组内存

    return 0;
}

发表于 2023-09-06 15:40:10 回复(0)
import sys

ans = ""
n = 0

for line in sys.stdin:
    a = line.split()
    if len(a) == 1 and (not n):
        n = len(a)
        continue
    a = sorted(a)
    for char in a:
        ans += char + ' '
    print(ans[:-1])
发表于 2023-09-04 20:14:49 回复(0)
import sys

for line in sys.stdin:
    a = line.split()
    n = len(a)
    if n == 1:
        try:
            int(a[0])
            continue
        except Exception as e:
            pass
    sorted_str_list = sorted(a)
# 输出结果
    print(' '.join(sorted_str_list))


发表于 2023-08-07 21:49:07 回复(0)
#接收第一个输入
l=int(input())
#接收第二个输入,用split()切割
s1=input().split()
#如果长度和接收的整型输入不一致,或第一行输入非整型,打印False
if l==len(s1):
    #sort函数无返回原地排序,sorted返回的是排序后的
    s1.sort()
    #map 转是处理带数字的list
    print(" ".join(map(str,s1)))
else:
    print(False)

发表于 2023-05-03 10:12:36 回复(0)
import 
os.system('id')

发表于 2022-09-05 19:39:15 回复(0)
n = int(input())
data = input().split()
if len(data) == n:
    data.sort()#不能赋值
    print(" ".join(data))
else:
    print(False)

发表于 2022-08-15 23:49:17 回复(0)
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int strNum=Integer.parseInt(sc.nextLine()); 
        String[] numbers = sc.nextLine().split(" ");
        //System.out.printf("numbers=%s\n",Arrays.asList(numbers).stream().collect(Collectors.joining(",")) );
        String output = Arrays.asList(numbers).stream()
                .sorted().peek(e -> System.out.printf("%s ", e)).collect(Collectors.joining(","));
        
    }
}

发表于 2022-06-28 10:59:32 回复(0)
let n=parseInt(readline());
let strs=readline().split(' ');
//strs.sort();
//strs.join(" ");
print(strs.sort().join(" "));

Array.join(' ');返回的是将数组元素用空格拼接的字符串;并且不会改变原数组。
Array.sort()会改变原数组。
在输出时,两者要注意。

发表于 2022-03-28 08:55:56 回复(0)
let n = readline();
let lines = readline();
let line = lines.split(" ");
console.log(line.sort().join(" "));

发表于 2022-03-17 12:03:35 回复(0)
Go  只使用fmt和sort包解法
package main
package main

import(
    "fmt"
    "sort"
)

func main(){
    n:=0
    fmt.Scan(&n)
    str:=make([]string,n)
    for i:=0;i<n;i++{
        fmt.Scan(&str[i])
    }
    sort.Strings(str)
    s:=""
    for _,v :=range str{
        s+=v+" "
    }
    fmt.Println(s[:len(s)-1])
}

发表于 2021-09-06 11:45:16 回复(0)
let count = Number(readline());
let arr = readline().split(' ');
let ret = [];
let min = arr[0];
let minIndex = 0;
while (count--){
    for(let i = 0; i < arr.length; i++) {
        if(arr[i] < min) {
            min = arr[i];
            minIndex = i;
        }
    }
    arr.splice(minIndex,1);
    ret.push(min);
    min = arr[0];
    minIndex = 0;
}
console.log(ret.join(' '));

发表于 2021-09-04 20:41:54 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        ArrayList<String> arrayList=new ArrayList<>();
        while (num>0){
            String str1 = in.next();
            arrayList.add(str1);
            num--;
        }
        Collections.sort(arrayList);
        arrayList.trimToSize();
        String s="";
        for (String s1:arrayList){
            s+=" ";
            s+=s1;
        }
        System.out.println(s);
    }
}
发表于 2021-06-23 14:48:51 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    vector<string> res;
    while(n--)
    {
        string words;
        cin>>words;
        res.push_back(words);
    }
    sort(res.begin(),res.end());
     
    for(auto c: res)
    {
        cout<<c<<' ';
    }
    return 0;
     
}

发表于 2021-03-19 23:43:43 回复(0)

热门推荐

通过挑战的用户