题解 | #两种排序方法#

两种排序方法

https://www.nowcoder.com/practice/839f681bf36c486fbcc5fcb977ffe432

两种排序方法

两种排序方法

/*
2022年09月20日 17:41:01
设置两个标记记录,当前序列排序方法情况
分别遍历去判断排序方法,如果同时遍历可能会互相影响
一旦判断不满足,要break出来,不需要再判断了。
*/

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<string> v(n);
    for (int i = 0; i < n; ++i)
        cin >> v[i];
    bool strsort = true, lensort = true; // 标记排序情况 默认是满足的
    for (int i = 1; i < n; ++i) { // 避免越界,i从1开始访问
        if (v[i - 1] > v[i]) {
            strsort = false;
            break;
        }
    }
    for (int i = 1; i < n; ++i) { // 避免越界,i从1开始访问
        if (v[i - 1].size() > v[i].size()) {
            lensort = false;
            break;
        }
    }
    if (strsort == true && lensort == false)
        cout << "lexicographically" << endl;
    else if (strsort == false && lensort == true)
        cout << "lengths" << endl;
    else if (strsort && lensort)
        cout << "both" << endl;
    else
        cout << "none" << endl;

    return 0;
}
全部评论

相关推荐

投递腾讯云智研发等公司7个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务