设为首页 加入收藏

TOP

C++排列对称串
2014-11-24 02:31:50 来源: 作者: 【 】 浏览:1
Tags:排列 对称

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

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

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

题目分析:

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

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

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

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

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

参考代码:



#include


#include


#include


#include


#include



using namespace std;



bool Comp(const string &s1,const string &s2)


{


return s1.length()!=s2.length() s1.length()

}


int main(int argc,char * argv[])


{


vector v;


string t,s;


while(cin>>s)


{


t=s;


reverse(t.begin(),t.end());


if(t==s)


{


v.push_back(s);


}


if(cin.get()=='\n')


{


break;


}


}


sort(v.begin(),v.end(),Comp);


for(int i=0;i

{


cout<

}


system("pause");


return 0;


}


运行结果:



推荐阅读


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Step By Step(Lua调用C函数) 下一篇C++实现按1的个数排序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: