C++中如何随机输出指定的字符串

就是将12个字符串随机输出成2组 1组6个 最好注释一下哪行是分组的 哪行是分组个数的

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
//GroupCount表示分组数,PerGroupCount表示每组字符串个数
const int GroupCount=2,PerGroupCount=6;
string strarr[GroupCount*PerGroupCount];
string group[GroupCount][PerGroupCount];
int i=0,j,k;

srand(time(NULL));
cout<<"请输入"<<(GroupCount*PerGroupCount)<<"个字符串:"<<endl;
while(i<GroupCount*PerGroupCount)
cin>>strarr[i++];
for(k=j=i=0;k<GroupCount*PerGroupCount;k++)
{
//如果产生的随机数是1且第1组分配的字符串未到6个,
//则将下标为k的字符串分给第1组
if(rand()%2&&i<PerGroupCount)
group[0][i++]=strarr[k];
else
group[1][j++]=strarr[k];
}
for(i=0;i<GroupCount;i++)
{
cout<<"第"<<(i+1)<<"组:"<<endl;
for(j=0;j<PerGroupCount;j++)
cout<<group[i][j]<<endl;
cout<<endl;
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答