1、随机化数组问题

就是对已有的数组进行乱序排列,使之随机的,毫无规律;
(1)、代码实现
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void showArray(int *a, int count);
void random_1(int *a, int count);
void random_1(int *a, int count){
int i;
int tmp;
int index;
srand(time(NULL));
for(i = count; i > 0; i--){
index = rand()%i;
tmp = a[index];
a[index] = a[i-1];
a[i-1] = tmp;
}
}
void showArray(int *a, int count){
int i;
for(i = 0; i < count; i++){
printf("%d ", a[i]);
}
printf("\n");
}
int main(void){
int a[] = {4, 6, 8, 2, 0, 7, 1,};
int count = sizeof(a)/sizeof(int);
showArray(a, count);
random_1(a, count);
showArray(a, count);
return 0;
}(2)、结果截图

2、约瑟夫环问题
M个元素,第N个元素出圈,从第start开始数即可;
(1)、代码实现
#include<stdio.h>
#include<malloc.h>
void yusf(char **str, int count, int doom, int start){
int *person;
int i;
int pre = start-2;
int cur = start-1;
int alive = count;
int doomNumber = 0;
if(start == 1){
pre = count-1;
}
person = (int *)malloc(sizeof(int) * count);
for(i = 0; i < count; i++){
person[i] = (i+1)%count; //循环数组
}
for(; alive > 0; cur = person[cur]){
if(++doomNumber >= doom){
printf("%s->出圈\n", str[cur]);
alive--;
doomNumber = 0;
person[pre] = person[cur]; //出圈时的pre的保存
}else{
pre = cur;
}
}
}
int main(void){
char *str[] = {"李大", "马二", "张三", "李四", "王五", "刘六", "吊七", "朱八", "杨九"};
int count = sizeof(str)/sizeof(char *);
int doom; //恶运数字
int start; //从第几个人开始
scanf("%d%d", &doom, &start);
if(doom > count || doom <= 0 || start > count|| start <= 0){
return;
}
yusf(str, count, doom, start); //count个元素,doom个厄运数字,从第start开始;
return 0;
}(2)、结果截图

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。