从数组中删除第m个元素 [英] Delete mth element from an array

查看:207
本文介绍了从数组中删除第m个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定大小为n的阵列,我需要一个写入其中删除该阵列中的每个第m个元素,直到该数组中只存在一个元素,并返回值的函数。
有人可以给我的提示吗?

Given an array of size n, I need to a write a function which deletes every mth element in the array till only one element exists in the array and return that value. Can somebody just give me the hints?

推荐答案

听起来你正在试图解决的约瑟夫斯问题

Sounds like you're trying to solve the Josephus Problem:

有个人站在一个圆圈
  等待执行。之后
  第一个男人被执行,若干
  人被跳过,一个人是
  执行。话又说回来,人
  跳过,而执行的人。该
  逛了一圈消除收益
  (这是变小
  作为执行的人将被删除),
  直到只剩下最后一个人仍然存在,谁
  给予自由。

There are people standing in a circle waiting to be executed. After the first man is executed, certain number of people are skipped and one man is executed. Then again, people are skipped and a man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains, who is given freedom.

的任务是选择在这个地方
  最初的圈子,让你生存
  (剩下的是最后一个)。

The task is to choose the place in the initial circle so that you survive (are the last one remaining).

维基制品包括以上一个非常简单的递归解决问题的方法:令n =人数中,k =人们每缺失跳过,则:

The wiki article includes a very simple recursive solution to the problem above: let n = number of people, k = people to skip per deletion, then:

f(1, k) = 0
f(n, k) = (f(n - 1, k) + k) % n

这篇关于从数组中删除第m个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆