C++之救济金发放问题

n(n<20)个人站成一圈,逆时针编号为1~n。有两个官员,A从1开始逆时针数,B从n开始顺时针数。在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上)。接下来被官员选中的人(1个或者2个)离开队伍。输入 n,k,m 输出每轮里被选中的人的编号

样例输入  

n=10 k=4 m=3

样例输出  

4 8 9 5 3 1 2 6 10 7

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int n, m, k;
    int i;
    int stemp_m, stemp_k;  // 代表军官AB所在的位置
    int people_num;   // 记录队伍中还有多少人未被选出
    int people_state[20] = {0}; 
    // 初始化数组所有值为0,
    // 当值为1时,代表该人存在,
    // 当值为0时,代表该人已被选出
 printf("请输入总人数n,官员A数的k,官员B数的m:"); scanf("%d %d %d", &n, &k, &m); for(i = 1; i <= n; i++) { people_state[i] = 1; } stemp_m = n + 1; stemp_k = 0; people_num = n; while(people_num) { for(i = 0; i < k; i++) // 得出官员A所选的人的位置  { do { stemp_k = (stemp_k + 1) % n; if(stemp_k == 0) { stemp_k = n; } } while (people_state[stemp_k] == 0); } for(i = 0; i < m; i++) // 得出官员B所选的人的位置  { do { stemp_m = stemp_m - 1; if(stemp_m == 0) { stemp_m = n; } } while (people_state[stemp_m] == 0); } printf("%d ", stemp_k); people_num--; if(stemp_m != stemp_k) { printf("%d ", stemp_m); people_num--; } people_state[stemp_m] = 0; people_state[stemp_k] = 0; } printf("\n"); system("pause"); }

猜你喜欢

转载自www.cnblogs.com/lzn-2018/p/11690545.html
今日推荐