• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

李必恒

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

报数

报数游戏是这样的:有n个人围成一圈,按顺序从1到n编好号。从第一个人开始报数,报到m(<n)的人退出圈子;下一个人从1开始报数,报到m的人退出圈子。如此下去,直到留下最后一个人。本题要求编写函数,给出每个人的退出顺序编号。其中n是初始人数;m是游戏规定的退出位次(保证为小于n的正整数)。函数CountOff将每个人的退出顺序编号存在数组out[]中。因为C语言数组下标是从0开始的,所以第i个位置上的人是第out[i-1]个退出的。

#include <stdio.h>
#define MAXN 20

void CountOff( int n, int m, int out[] );

int main()
{
int out[MAXN], n, m;
int i;

scanf("%d %d", &n, &m);
CountOff( n, m, out );
for ( i = 0; i < n; i++ )
printf("%d ", out[i]);
printf("\n");

return 0;
}

void CountOff( int n, int m, int out[] )
{
int i=0,count=0,cnt=0;
for(int i=0;i<n;i++)
out[i]=-1;
while(count!=n)
{
if(out[i]==-1)
cnt++;
if(cnt==m)
{
count++;
out[i]=count;
cnt=0;
}
if(i==n-1&&count!=n)
i=0;
else
i++;
}
return ;
}

 

posted on 2021-07-30 11:01  李必恒  阅读(919)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3