当前位置:网站首页>Niu Ke's questions -- two sorting methods

Niu Ke's questions -- two sorting methods

2022-06-11 18:30:00 HHYX.

Topic link : Two sort methods

Title Description

Koala has n A string string , Any two strings are different in length . Koala recently learned that there are two ways to sort strings :
1. Sort strings in lexicographic order . for example :
“car” < “carriage” < “cats” < “doggies < “koala”
2. Sort according to the length of the string . for example :
“car” < “cats” < “koala” < “doggies” < “carriage”
Koalas want to know whether their string order meets these two sorting methods , Koalas are busy eating leaves , So I need you to help verify .
Input description :
Enter the number of first line strings n(n ≤ 100) Next n That's ok , One string per line , String length is less than 100, It's all made up of small letters
Output description :
If these strings are arranged according to dictionary order rather than length "lexicographically”,
If the output is arranged according to length rather than dictionary order "lengths",
If both methods match the output "both", Otherwise output "none"

 Insert picture description here

Topic analysis

According to the meaning of the question, a bunch of strings are given here , You need to determine whether the strings are sorted by length or by dictionary order . Dictionary order is the order in which strings are compared in size . Length compares the length of each string . So here we can judge separately , Use one bool Function of type , If the dictionary order is always satisfied ( Length order ) You can return true, In case of dissatisfaction, it returns false. The final output can be made according to whether dictionary order or length order is satisfied or both . The code implementation is as follows :

Code implementation

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

bool IsLengths(const vector<string>& v)
{
    
    for(size_t i=0;i<v.size() - 1;i++)
    {
    
        if(v[i].size()>v[i+1].size())// Judge whether the previous one is smaller than the latter one 
        {
    
            return false;
        }
    }
    return true;
}


bool Islexicographically(const vector<string>& v)
{
    
    for(size_t i=0;i<v.size() - 1;i++)
    {
    
        if(v[i] > v[i+1])// Judge whether the previous one is smaller than the latter one 
        {
    
            return false;
        }
    }
    return true;
}



int main()
{
    
    vector<string> v;
    int n=0;
    cin >> n ;
    v.resize(n);// Set up v The number of elements in 
    for(size_t i=0;i<v.size();i++)
    {
    
        cin>>v[i];// Enter individual strings 
    }
    bool lsort = IsLengths(v);// Judge whether it is length order 
    bool csort = Islexicographically(v);// Judge whether it is a dictionary order 
    if(lsort && csort)
    {
    
        cout<<"both"<<endl;
    }
    else if(lsort)
    {
    
        cout<<"lengths"<<endl;
    }
    else if(csort)
    {
    
        cout<<"lexicographically"<<endl;
    }
    else
    {
    
        cout<<"none"<<endl;
    }
    return 0;
}

 Insert picture description here

原网站

版权声明
本文为[HHYX.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111814366606.html