题解 | #求长方体表面积#

字符串排序

http://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723

#include<bits/stdc++.h>
using namespace std;
//按照字典序排序,string直接比大小,可以用multiset直接输出
int main(){
    string strInput="ssr";
    int num = 0;
    cin>>num;
    vector<string> myVec;//不能初始化为{""},导致第一个元素存在且为空
//     while(getline(cin, strInput)){//为什么第一个是空字符串?第一个已经用过了,所以为空了?
//         myVec.push_back(strInput);  
//     }
    for(int i = 0; i<num; ++i){
        cin>>strInput;
        myVec.push_back(strInput);
    }
    for(int i = 0; i<num; ++i){
        for(int j = 0; j<num-i-1; ++j){
            if (myVec[j]>myVec[j+1]) swap(myVec[j], myVec[j+1]);
        }
    }
    for(auto str:myVec){
        cout<<str<<endl;
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
FieldMatching:看成了猪头顾问,不好意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务