首页 > 试题广场 >

字符串排序

[编程题]字符串排序
  • 热度指数:10277 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。 如果在输入过程中输入的一个字符串为“stop”,也结束输入。 然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。

输入描述:
字符串的个数,以及该组字符串。每个字符串以‘\n’结束。如果输入字符串为“stop”,也结束输入.


输出描述:
可能有多组测试数据,对于每组数据,
将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。

根据输入的字符串个数来动态分配存储空间(采用new()函数)。每个字符串会少于100个字符。
测试数据有多组,注意使用while()循环输入。
示例1

输入

5
sky is grey
cold
very cold
stop
3
it is good enough to be proud of
good
it is quite good

输出

cold
very cold
sky is grey
good
it is quite good
it is good enough to be proud of
头像 Coming680
发表于 2022-03-25 21:37:15
#include<iostream> #include<vector> #include<algorithm> #include<cstdio> using namespace std; int main() { int n; stri 展开全文
头像 健康快乐最重要
发表于 2020-03-05 12:40:40
用了结构体,可能就更清晰了。然后配合着sort的排序。本身没有什么复杂的地方。注意:cin输入后,后边会保留一个换行符,然后getline忽略空格时,就会造成换行符被读入。所以在cin之后用getchar吸收换行符。引用大佬的话: #include<iostream> #include 展开全文
头像 牛客440904392号
发表于 2024-10-02 12:43:42
//Java版代码 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); wh 展开全文
头像 斩题怪
发表于 2020-04-13 01:14:52
map映射底层是用红黑树来实现的,可以使映射有序,所以此题就可以使用map,将字符串逐个添加至映射中,最后按序输出即可,即省去了对字符串进行存储,也省去了排序的步骤 #include <cstdio> #include <iostream> #inclu 展开全文
头像 烤肉__
发表于 2022-01-23 18:34:07
用最简单的排序做 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; bool cmp(string &a 展开全文
头像 lyw菌
发表于 2023-03-11 16:13:19
//sort函数对输入的字符串长度排序 #include "stdio.h" #include "string" #include "algorithm" using namespace std; struct charNode{ int length; string str; }; 展开全文
头像 牛客921131199号
发表于 2025-03-15 14:12:02
#include<bits/stdc++.h> using namespace std; bool cmp(string a, string b) { return a.size() < b.size(); } int main() { int n; whi 展开全文
头像 1231b
发表于 2023-03-16 10:46:07
#include <iostream> #include <string> using namespace std; struct StringLength { string word; int lengths; }; int main() { int 展开全文
头像 chong_0428
发表于 2024-03-21 23:09:04
def leng(s): a = [] for i in s: l = len(i) a.append(l) # print(a) return a def str_sort(s): flag = True a = len 展开全文
头像 牛客588682307号
发表于 2024-03-17 15:46:20
# include<stdio.h> #include<iostream> using namespace std; struct stu { string s; int length; }; int main() { int n; whil 展开全文