写一个简单的随即数字,要求随即7个数字,而其个不相同求~
1:
产生一个后去和以前产生过的数字比较 看是否相同 相同就重新生成
2:
随机生成一个“最小数” 然后以增值的方式再去产生其他的数
看具体情况 不同的数字要求有不同的生成方法
void jx36(){
int j;
int sjsh[7];
for(j = 0; j < 7; j++){
sjsh[j] = rand()%37;
switch(j){
case 0:;break;
case 1:
{
if(sjsh[j]==sjsh[j-1])
j--;
};break;
case 2:
{
if(sjsh[j]==sjsh[j-1]||sjsh[j]==sjsh[j-2])
j--;
};break;
case 3:
{
if(sjsh[j]==sjsh[j-1]||sjsh[j]==sjsh[j-2]||sjsh[j]==sjsh[j-3])
j--;
};break;
case 4:
{
if(sjsh[j]==sjsh[j-1]||sjsh[j]==sjsh[j-2]||sjsh[j]==sjsh[j-3]||sjsh[j]==sjsh[j-4])
j--;
};break;
case 5:
{
if(sjsh[j]==sjsh[j-1]||sjsh[j]==sjsh[j-2]||sjsh[j]==sjsh[j-3]||sjsh[j]==sjsh[j-4]||sjsh[j]==sjsh[j-5])
j--;
};break;
case 6:
{
if(sjsh[j]==sjsh[j-1]||sjsh[j]==sjsh[j-2]||sjsh[j]==sjsh[j-3]||sjsh[j]==sjsh[j-4]||sjsh[j]==sjsh[j-5]||sjsh[j]==sjsh[j-6])
j--;
};break;
}
printf("%d ", sjsh[j]);
} srand((unsigned)time(NULL));
printf("\n");
for (int k = 0; k<7; k++){
for (int j = 6; j>k; j--){
if(sjsh[j]<sjsh[j-1]){
sum=sjsh[j];
sjsh[j]=sjsh[j-1];
sjsh[j-1]=sum;
}
}
}
for (int j = 0; j<7; j++){
printf("%d ", sjsh[j]);
}
}
就是这样吧