写一个简单的随即数字,要求随即7个数字,而其个不相同求~
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]);
}
}
就是这样吧