当前位置:网站首页>【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;
}
边栏推荐
- 金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)
- 创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
- 【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
- 研发团队数字化转型实践
- 开发工具:第五章:使用idea生成实体类
- QT_QDialog dialog
- CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) Solution
- 深圳市商务局2022年度中央资金(跨境电子商务企业市场开拓扶持事项)申报指南
- 极化微波成像概述2
- Detailed explanation of the working principle of crystal oscillator
猜你喜欢

棕榈油罐区数字化转型

GRUB2的零日漏洞补丁现已推出

B011 - 51-based multifunctional fingerprint smart lock
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching

QT_QThread线程

The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!

XAML WPF item groupBox control

不需要写代码,快速批量修改文件夹中图片的格式

实现mnist手写数字识别

【R语言】对图片进行裁剪 图片批量裁剪
随机推荐
The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
移动端吸顶方案
面经汇总-社招-6年
2022年SQL经典面试题总结(带解析)
02 es cluster construction
变量交换;复合赋值;增递减运算符
The anxiety of the post-90s was cured by the vegetable market
RecSys'22|CARCA: Cross-Attention-Aware Context and Attribute Recommendations
AIOps智能运维的领跑者擎创科技正式入驻InfoQ 写作社区!
GTK修改pixmap像素,提取pixmap像素RGB值
云商店携手快报税,解锁财务服务新体验!
关于LocalDateTime的全局返回时间带“T“的时间格式处理
【R语言】批量重命名文件
浅谈大数据背景下数据库安全保障体系
三维空间中点的插值
插入排序 优化插入排序
程序员架构修炼之道:如何设计“易理解”的系统架构?
【100个网络运维工作者必须知道的小知识!】
B001 - 基于STM32的智能生态鱼缸
Topology Parts Disassembly 3D Visualization Solution