数学游戏:猴子围圈数数求大王问题解析

版权申诉
0 下载量 156 浏览量 更新于2024-11-11 收藏 24KB RAR 举报
资源摘要信息: "约瑟夫问题(Josephus Problem)求解与算法实现" 约瑟夫问题(Josephus Problem),又称约瑟夫斯环或约瑟夫环问题,是一个著名的数学问题,涉及数学中的组合学、算法设计与分析等领域。约瑟夫问题源自于一个描述性的故事,讲述的是m个猴子围坐一圈,按照一定规则轮到的猴子将被移出圈子,直到最后剩下一只猴子,这只猴子被称作大王。问题的核心在于确定这只“大王猴子”的位置。 问题的描述与算法实现可以分解为以下几个步骤: 1. 初始化:首先需要对m个猴子进行编号,这个编号可以是1到m的整数序列。这些猴子围坐成一个圆圈。 2. 规则设定:从编号为1的猴子开始,按顺时针方向数数,每数到第N个猴子,就将该猴子移出圈子。这里的N是一个关键参数,它决定了数数的间隔。 3. 模拟过程:按照规则进行迭代,每次从被移出圈子的猴子后一个猴子重新开始数数,直到圆圈中只剩下一只猴子为止。 4. 求解“大王猴子”编号:随着过程的进行,需要记录每只被移出圈子的猴子的位置,这样可以通过数学计算或其他算法手段确定最终剩下的那只猴子的编号。 数学解法通常涉及到递推公式或者直接使用数学公式计算。例如,可以通过以下递推关系得到最终结果: 令 f(m, n) 表示m只猴子,每数到第n个猴子时就移出一个,最终剩下一只猴子的编号。则有如下递推关系: f(m, n) = (f(m-1, n) + n) mod m, 当 m > 1 f(1, n) = 0,因为只有一个猴子时,它就是“大王猴子”。 这里,mod表示取模运算,保证编号始终为正整数。 除了数学解法之外,还可以使用编程算法来模拟整个过程。比较常见的编程语言实现包括使用循环和队列(或者数组)来模拟猴子的位置变化,并用计数器来跟踪当前轮到的猴子编号。 例如,在C++中可以这样模拟: ```cpp #include <iostream> using namespace std; int josephus(int m, int n) { if (m == 1) return 0; else //递归公式 return (josephus(m - 1, n) + n) % m; } int main() { int m = 10; // 猴子总数 int n = 3; // 数到第3个猴子时移出 cout << "最后的大王猴子的编号是: " << josephus(m, n) << endl; return 0; } ``` 在这个C++程序中,函数`josephus`使用递归的方式来计算最后剩下的猴子的编号。注意,这里n不能为1,因为那样的话会进入无限循环,而当m=1时,由于只剩下一只猴子,所以直接返回编号0作为结果。 约瑟夫问题还可以进一步推广,例如考虑猴子坐在多维空间中的情况,或者考虑猴子移出顺序不是简单地按照数到的顺序而是有更复杂的规则时的解法。 综上,约瑟夫问题不仅是数学中的一个有趣问题,也是编程实现中的一个经典算法题,它能够帮助学生和程序员锻炼逻辑思维和算法设计的能力。

java.sql.SQLException: sql injection violation, syntax error: TODO : pos 872, line 15, column 43, token UNION : SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE to_char(asewo.plan_start_time_T,'yyyy_mm-dd hh24:mi') <= to_char(plan_finish_time_T,'yyyy_mm_dd hh24:mi') AND asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=? ORDER BY asewo.creation_time DESC Union All SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=? ORDER BY asewo.creation_time DESC如何改

2023-06-13 上传

改如何修正: <select id="getCurrentTask" resultType="com.sottop.sokonmobile.sokonmobile.qingdao.entity.AsEmWorkOrderEntity"> SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE to_char(asewo.plan_start_time_T,'yyyy_mm-dd hh24:mi') <= to_char(plan_finish_time_T,'yyyy_mm_dd hh24:mi') AND asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=#{executor} <if test="orderType!=null and orderType!=''"> AND asewo.order_type_s=#{orderType} </if> ORDER BY asewo.creation_time DESC Union All SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=#{executor} <if test="orderType!=null and orderType!=''"> AND asewo.order_type_s=#{orderType} </if> ORDER BY asewo.creation_time DESC </select>

2023-06-13 上传