偷偷回答一下,
#include?<iostream>#include?<string>
#include?<time.h>
using?namespace?std;
void?randWord(string?&?word)?{
//?rand()?%?9?的取值范围是?0~8
//?所以?12?+?rand()?%?9?的取值范围就是12~20
int?len?=?12?+?rand()?%?9;
word.assign(len,?'\0');
//?给“单词”的每一个字母赋值
for(int?i=0;?i<len;?i++)?{
//?rand()?%?2?的取值范围是0~1,0表示小写,1表示大写
//?相当于随机取大小写
bool?isUpper?=?rand()?%?2;
if?(isUpper)?{
word[i]?=?'A'?+?rand()?%?26;
}?else?{
word[i]?=?'a'?+?rand()?%?26;
}
}
}
int?main()?{
srand(time(NULL));
const?int?N?=?20;
string?words[N];
for(int?i=0;?i<N;?i++)?{
randWord(words[i]);
}
sort(words,?words+N);
for(int?i=0;?i<N;?i++)?{
cout?<<?words[i]?<<?endl;
}
return?1;
}