对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
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] + " "); } } } }
#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; }
#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; }
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))
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(",")); } }
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(' '));