3517 And Then There Was One 约瑟夫问题

本文介绍了一个有趣的算法问题:从一圈编号的石头中,遵循特定规则移除石头,直至剩下最后一块。通过示例详细解释了游戏规则,并提供了一段C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

And Then There Was One
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3136 Accepted: 1562

Description

Let’s play a stone removing game.

Initially, n stones are arranged on a circle and numbered 1, …, n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m. In step 2, locate the k-th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k − 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1.


Initial state

Step 1

Step 2

Step 3

Step 4

Step 5

Step 6

Step 7

Final state
 
Figure 1: An example game

Initial state: Eight stones are arranged on a circle.

Step 1: Stone 3 is removed since m = 3.

Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8.

Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case.

Steps 4–7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7.

Final State: Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1.

Input

The input consists of multiple datasets each of which is formatted as follows.

n k m

The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions.

2 ≤ n ≤ 10000, 1 ≤ k ≤ 10000, 1 ≤ mn

The number of datasets is less than 100.

Output

For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output.

Sample Input

8 5 3
100 9999 98
10000 10000 10000
0 0 0

Sample Output

1
93
2019
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int n,k,m;//第一次move第m个,剩下的move后数第k个
    while(scanf("%d%d%d",&n,&k,&m)==3&&n)
    {
        int s=0;
        for(int i=2;i<=n;i++)
        {
            if(i==n) s=(s+m)%i;
            else s=(s+k)%i;
        }
        printf("%d/n",s+1);
    }
    return 0;
}
### SQL 中 `CASE WHEN AND THEN` 的语法与示例 #### 1. **带 `AND` 条件的 `CASE WHEN`** 当需要在 `CASE WHEN` 中引入多个条件时,可以使用逻辑运算符 `AND`。以下是具体语法结构: ```sql CASE WHEN condition1 AND condition2 THEN result1 WHEN condition3 AND condition4 THEN result2 ... ELSE default_result END ``` 示例:假设我们有一个名为 `employees` 的表,记录了员工的信息(如年龄、工资等),现在需要根据员工的年龄和工资水平对其进行分类。 ```sql SELECT employee_id, age, salary, CASE WHEN age >= 30 AND salary > 5000 THEN 'Senior High Income' WHEN age >= 30 AND salary <= 5000 THEN 'Senior Low Income' WHEN age < 30 AND salary > 5000 THEN 'Junior High Income' WHEN age < 30 AND salary <= 5000 THEN 'Junior Low Income' ELSE 'Other' END AS category FROM employees; ``` 在此查询中,`CASE` 同时考虑了两个维度——年龄 (`age`) 和收入 (`salary`),并将其划分为不同的类别[^1]。 --- #### 2. **实际应用场景** 下面是一个更加具体的例子,展示如何利用 `CASE WHEN AND THEN` 对学生考试成绩进行精细化分析: 数据准备: ```sql CREATE TABLE exam_results ( student_id INT, subject VARCHAR(50), score DECIMAL(5, 2) ); INSERT INTO exam_results VALUES (1, 'Math', 85.5), (2, 'English', 72.0), (3, 'Science', 90.0); ``` 查询逻辑: ```sql SELECT student_id, subject, score, CASE WHEN subject = 'Math' AND score >= 90 THEN 'A+' WHEN subject = 'Math' AND score >= 80 THEN 'A' WHEN subject = 'Math' AND score >= 70 THEN 'B' WHEN subject = 'English' AND score >= 85 THEN 'Distinction' WHEN subject = 'English' AND score >= 70 THEN 'Pass' ELSE 'Fail' END AS grade FROM exam_results; ``` 解析:该查询针对不同科目设置了独立的成绩评定标准,并通过组合条件实现了精确的结果映射[^2]。 --- #### 3. **结合聚合函数的应用** 我们也可以将 `CASE WHEN AND THEN` 应用于聚合计算中。例如统计某公司各部门高薪员工的比例。 数据准备: ```sql CREATE TABLE department_salaries ( department_name VARCHAR(50), employee_salary DECIMAL(10, 2) ); INSERT INTO department_salaries VALUES ('HR', 6000), ('Engineering', 12000), ('Marketing', 8000); ``` 聚合查询: ```sql SELECT department_name, COUNT(*) AS total_employees, SUM(CASE WHEN employee_salary > 10000 AND department_name != 'HR' THEN 1 ELSE 0 END) AS high_income_count FROM department_salaries GROUP BY department_name; ``` 此处,`CASE` 不仅限定了薪水范围 (>10000),还排除了特定部门 ("HR") 的干扰[^3]。 --- #### 4. **注意事项** - 使用 `AND` 组合条件时需注意顺序,确保最严格的筛选放在前面以提高效率。 - 若存在重复匹配的情况,则只有第一个满足条件的部分会被返回,其余部分将被忽略[^4]。 - 复杂条件下建议适当拆分逻辑层次,提升可维护性和清晰度。 --- ### 总结 通过合理运用 `CASE WHEN AND THEN` 构造多维条件判断,能够显著增强 SQL 查询的能力,适用于多种业务场景下的数据分析需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值