都去上早操!
这是一个随机选人程序,可以从指定的n个人中随机选出m位去做早操。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <stdio.h> #include <stdlib.h> #include <time.h>
int main() { char *people[] = {"王星", "王柏", "莫奈", "姚祉", "肖鑫", "石安"}; int n, i, temp; int selected[6] = {0}; srand((unsigned int)time(NULL)); printf("请输入要抽取的人数(1-6):"); scanf("%d", &n); if (n < 1 || n > 6) { printf("输入错误!请输入1到6之间的数字。\n"); return 1; } printf("抽取结果:"); for (i = 0; i < n; i++) { do { temp = rand() % 6; } while (selected[temp] == 1); selected[temp] = 1; printf("%s ", people[temp]); } printf("\n"); return 0; }
|