当前位置:网站首页>【Day_08 0426】两种排序方法
【Day_08 0426】两种排序方法
2022-08-01 17:40:00 【安河桥畔】
两种排序方法
题目来源
牛客网:两种排序方法
题目描述
考拉有n个字符串字符串,任意两个字符串长度都是不同的。考拉最近学习到有两种字符串的排序方法: 1.根据字符串的字典序排序。例如:
“car” < “carriage” < “cats” < "doggies < “koala”
2.根据字符串的长度排序。例如:
“car” < “cats” < “koala” < “doggies” < “carriage”
考拉想知道自己的这些字符串排列顺序是否满足这两种排序方法,考拉要忙着吃树叶,所以需要你来帮忙验证。
输入描述
输入第一行为字符串个数n(n ≤ 100) 接下来的n行,每行一个字符串,字符串长度均小于100,均由小写字母组成
输出描述
如果这些字符串是根据字典序排列而不是根据长度排列输出"lexicographically",
如果根据长度排列而不是字典序排列输出"lengths",
如果两种方式都符合输出"both",否则输出"none"
示例
输入
3
a
aa
bbb输出
both
思路分析
- 要输入多个字符串,所以使用vector<string>来进行保存
- 遍历vector,比较前后两个字符串的长度和大小
- C语言中字符串比较用strcmp函数,C++中可以直接使用’>'比较两个字符串的大小
代码展示
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> ar;
int n = 0;
cin >> n;
ar.resize(n);
for (int i = 0; i < n; i++)
{
cin >> ar[i];
}
bool lex = true;
bool lengths = true;
for (int i = 0; i < n - 1; i++)
{
//lengths
if (ar[i].size() > ar[i + 1].size())
{
lengths = false;
}
//lexicographically
if (ar[i] > ar[i + 1])
{
lex = false;
}
//如果两种排序都不是,提前break
if (!(lengths) && !(lex))
{
break;
}
}
if (lex && lengths)
{
cout << "both" << endl;
}
else if (lex && !(lengths))
{
cout << "lexicographically" << endl;
}
else if (lengths && !(lex))
{
cout << "lengths" << endl;
}
else
{
cout << "none" << endl;
}
return 0;
}
边栏推荐
- golang json 返回空值
- 晶振工作原理详解
- 2022年深圳市临床医学研究中心申请指南
- 中信证券是国内十大券商吗?怎么开户安全?
- C# LibUsbDotNet 在USB-CDC设备的上位机应用
- 解决MySQL插入不了中文数据问题
- 我在启牛开户安全吗?谁能告诉我开不靠谱?
- QT基础功能,信号、槽
- 创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
- How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
猜你喜欢
随机推荐
【二叉树】奇偶树
Sftp中文件名乱码
云商店携手快报税,解锁财务服务新体验!
opencv real-time face detection
Isometric graph neural networks shine in drug discovery
M1芯片电脑安装cerebro
指针和解引用
Are online account opening commissions reliable? Is online account opening safe?
快速抽取resnet_v2_152中间的特征层
Unity ui点击事件只响应最上层ui的方式
opencv语法Mat类型总结
2022年MySQL最新面试题
XAML WPF item groupBox control
How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
金仓数据库KingbaseES安全指南--6.4. RADIUS身份验证
力扣每日一题-第45天-697. 数组的度
银行案例|Zabbix跨版本升级指南,4.2-6.0不香吗?
Topology Parts Disassembly 3D Visualization Solution
极化微波成像概述2
2022年深圳市临床医学研究中心申请指南









