题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/dfeed0e0e4624814b122265e859783b2
#include<iostream> #include<cmath> #include<algorithm> using namespace std; struct chuan{ string str; int len; }a[100]; bool comp(chuan a,chuan b){ if(a.len<b.len){ return true; } else return false; } int main(){ int n; while(scanf("%d",&n)!=EOF){ cin.ignore(); for(int i=0;i<=n;i++){ getline(cin,a[i].str); if(a[i].str=="stop"){ n=i; break; } a[i].len=a[i].str.size(); } sort(a,a+n,comp); //排序 for(int i=0;i<=n;i++){ if(a[i].str!="stop"){ cout<<a[i].str<<endl; } } } return 0; }