
【高分悬赏~】C语言的一个关于随机抽取的问题
有六个字母a-f,每一次随机抽取其中三个字母(例如:abc,cdf,def..单次抽取中三个字母不能重复),然后重复以上操作,当第N次后,刚好取遍所有的a-f六个字母。输...
有六个字母a-f,每一次随机抽取其中三个字母(例如:abc,cdf,def..单次抽取中三个字母不能重复),然后重复以上操作,当第N次后,刚好取遍所有的a-f六个字母。输出N值。
然后按照上面方法,重复运行,输出多个N值(因为是随机抽所以N是变量)。
结束。
希望高手能给出全码~~~~~ 展开
然后按照上面方法,重复运行,输出多个N值(因为是随机抽所以N是变量)。
结束。
希望高手能给出全码~~~~~ 展开
2个回答
展开全部
除去代码中间的注释可以看到选字母的过程.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#define TIME 10 //运行TIME次
int main(){
time_t t;
time(&t);
srand(t);
char staout[256];
char stain[256];
int ti;
for(ti=0;ti<TIME;++ti){
memset(staout,0,256);
char c;
int i;
int count=0;
int numout=0;
while(numout<6){
int numin=0;
memset(stain,0,256);
while(numin<3){
do{
c=rand()%6+'a';
}
while(stain[c]==1);
stain[c]=1;
// printf("%c ",c); //去掉这里和下面的注释可以看到过程
++numin;
}
// printf("\n");
for(i='a';i<='f';++i){
if(stain[i]==1){
if(staout[i]==0){
++numout;
}
staout[i]=1;
}
}
++count;
}
printf("%d\n",count);
}
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#define TIME 10 //运行TIME次
int main(){
time_t t;
time(&t);
srand(t);
char staout[256];
char stain[256];
int ti;
for(ti=0;ti<TIME;++ti){
memset(staout,0,256);
char c;
int i;
int count=0;
int numout=0;
while(numout<6){
int numin=0;
memset(stain,0,256);
while(numin<3){
do{
c=rand()%6+'a';
}
while(stain[c]==1);
stain[c]=1;
// printf("%c ",c); //去掉这里和下面的注释可以看到过程
++numin;
}
// printf("\n");
for(i='a';i<='f';++i){
if(stain[i]==1){
if(staout[i]==0){
++numout;
}
staout[i]=1;
}
}
++count;
}
printf("%d\n",count);
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询