zoukankan      html  css  js  c++  java
  • C++排列对称串

    题目内容:字符串有些是对称的,有些是不对称的,请将那些对称的字符串按从小到大的顺序输出。字符串先以长度论大小,如果长度相同,再以ASCII码值为排序标准。

    输入描述:输入数据中含有一些字符串(1<=串长<=256)

    输出描述:根据每个字符串,输出对称的那些串,并且要求按从小到大的顺序输出。

    题目分析:

    (1)定义一个string类型的向量容器

    (2)将输入的字符串反转,看是否和原字符串相同,以此判断字符是否对称

    (3)若对称,则将该字符串插入到向量容器中

    (4)使用sort算法对向量元素排序,自己设计排序比较函数,把这个函数指定给sort算法

    排序比较函数的具体方法是,判断参与比较的没两个字符串的长度是否相等,若不相等则按长度从小到大的顺序返回;若相等则以ASCII码值为排序标准,即按字符串从小到大排序

    参考代码:

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    bool Comp(const string &s1,const string &s2)
    {
        return s1.length()!=s2.length()?s1.length()<s2.length():s1<s2;
    }
    int main(int argc,char * argv[])
    {
        vector<string> v;
        string t,s;
        while(cin>>s)
        {
            t=s;
            reverse(t.begin(),t.end());
            if(t==s)
            {
                v.push_back(s);
            }
            if(cin.get()=='
    ')
            {
                break;
            }
        }
        sort(v.begin(),v.end(),Comp);
        for(int i=0;i<v.size();i++)
        {
            cout<<v[i]<<endl;
        }
        system("pause");
        return 0;
    }
    

    运行结果:

  • 相关阅读:
    模块三
    python的6种基本数据类型--字典
    python的6种基本数据类型--集合
    数据集之集合
    锚点使用
    JSON返回DateTime/Date('123123123')/解决办法
    JSON返回DateTime/Date('123123123')/解决办法
    设计模式-系列索引
    设计模式-系列索引
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/cysolo/p/3381291.html
Copyright © 2011-2022 走看看