题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/dfeed0e0e4624814b122265e859783b2
#include <iostream> #include <string> using namespace std; struct StringLength { string word; int lengths; }; int main() { int n; while(cin>>n) { StringLength* a=new StringLength[n]; StringLength b; int k=0; getline(cin,a[0].word); for(int i=0;i<n;i++) { getline(cin,a[i].word); if(a[i].word=="stop") break; a[i].lengths=a[i].word.length(); k++; } for(int i=0;i<k;i++) { for(int j=0;j+1<k-i;j++) { if(a[j].lengths>a[j+1].lengths) { b=a[j]; a[j]=a[j+1]; a[j+1]=b; } } } for(int i=0;i<k;i++) { cout<<a[i].word<<endl; } } } // 64 位输出请用 printf("%lld")