Counting-out Rhyme<vector实现约瑟夫环模拟>

本文介绍了一种计数淘汰游戏的算法实现过程,n名儿童围成一圈按顺时针方向编号从1到n,通过k轮计数淘汰确定每轮被淘汰儿童的编号。文章提供了完整的C++代码示例,可用于理解和实现该算法。

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

n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.

For example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and ai = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.

You have to write a program which prints the number of the child to be eliminated on every step.

Input

The first line contains two integer numbers n and k (2 ≤ n ≤ 1001 ≤ k ≤ n - 1).

The next line contains k integer numbers a1, a2, ..., ak (1 ≤ ai ≤ 109).

Output

Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.

Examples
input
7 5
10 4 11 4 1
output
4 2 5 6 1 
input
3 2
2 5
output
3 2 
Note

Let's consider first example:

  • In the first step child 4 is eliminated, child 5 becomes the leader.
  • In the second step child 2 is eliminated, child 3 becomes the leader.
  • In the third step child 5 is eliminated, child 6 becomes the leader.
  • In the fourth step child 6 is eliminated, child 7 becomes the leader.
  • In the final step child 1 is eliminated, child 3 becomes the leader.


代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[100+10];
int main()
{
    LL n,m;
    vector<int>q;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        q.push_back(i);
    int ans=0;
    for(int i=1; i<=m; i++)
    {
        cin>>a[i];
        int x=(a[i]%q.size()+ans)%q.size();
        cout<<q[x]<<" ";
        ans=x;
        q.erase(q.begin()+x);
    }
    return 0;
}


<think>好的,我现在需要帮用户解决如何对Java中的List<Map<String, Object>>进行汇总和计数操作。首先,我得理解用户的需求。用户有一个包含多个Map的List,每个Map中的键是字符串,值是Object类型。他们想要对这些数据进行汇总和计数,比如统计某个字段出现的次数,或者根据某些条件分组求和。 首先,我需要考虑传统的Java 7及之前的方法,因为用户可能还在使用旧版本的Java。这时候,通常会通过遍历List,然后逐个Map处理,使用一个临时的Map来保存统计结果。比如,统计某个键对应的值的出现次数,可以用一个HashMap,键是目标字段的值,值是出现次数。每次遍历时检查并更新这个临时Map。 接下来,用户可能使用的是Java 8及以上版本,这时候可以利用Stream API来简化操作。Stream的collect和groupingBy方法非常适合这种分组统计。例如,使用Collectors.groupingBy按某个键分组,然后用Collectors.counting()统计次数。这样代码会更简洁,效率也可能更高。 然后,我需要考虑不同的统计场景。比如,简单的计数,用户可能想统计某个字段每个值出现的次数。这时候,用groupingBy和counting组合即可。如果是多条件分组,比如根据多个字段组合作为键,就需要在groupingBy中使用更复杂的键,比如一个包含多个字段的List或者自定义对象。 另外,用户可能有求和的需求,比如某个数值字段需要根据另一个字段分组求和。这时候需要用Collectors.summingInt或summingDouble等方法。如果字段是Object类型,可能需要先进行类型转换,确保可以正确计算。 还要注意处理可能的null值。如果Map中的某些键可能不存在,或者值为null,需要决定是否跳过这些情况,或者在统计时包含它们。比如,使用getOrDefault方法,或者在Stream处理中过滤掉null值。 然后,我需要考虑如何展示结果。统计后的结果通常是一个Map,其中键是分组的依据,值是统计结果。用户可能需要遍历这个结果Map来输出或进一步处理。 最后,可能需要提供一些示例代码,帮助用户更直观地理解如何实现这些操作。比如,传统方式的代码示例和Stream API的示例,并解释每个步骤的作用。同时,提醒用户注意类型转换的问题,尤其是Object转换为具体的数值类型时可能需要处理异常情况。 总结来说,解决方案需要涵盖不同Java版本的实现方式,处理单字段和多字段分组,计数和求和等不同操作,并注意处理可能的异常情况和null值。通过分步骤的说明和示例代码,用户应该能够根据自己的具体需求选择合适的实现方法。</think>在Java中对`List<Map<String, Object>>`进行汇总计数,可以通过以下步骤实现--- ### **一、需求场景分析** 假设数据结构如下: ```java List<Map<String, Object>> dataList = new ArrayList<>(); dataList.add(Map.of("category", "A", "value", 10)); dataList.add(Map.of("category", "B", "value", 20)); dataList.add(Map.of("category", "A", "value", 5)); ``` 需要实现: 1. 按`category`分组计数 2. 按`category`分组求和`value`字段 --- ### **二、传统遍历实现(Java 7及以下)** ```java // 1. 分组计数 Map<String, Integer> countMap = new HashMap<>(); for (Map<String, Object> item : dataList) { String category = (String) item.get("category"); countMap.put(category, countMap.getOrDefault(category, 0) + 1); } // 2. 分组求和 Map<String, Integer> sumMap = new HashMap<>(); for (Map<String, Object> item : dataList) { String category = (String) item.get("category"); Integer value = (Integer) item.get("value"); sumMap.put(category, sumMap.getOrDefault(category, 0) + value); } ``` --- ### **三、使用Stream API实现(Java 8+)** #### 1. **分组计数** ```java Map<String, Long> countMap = dataList.stream() .collect(Collectors.groupingBy( item -> (String) item.get("category"), Collectors.counting() )); ``` #### 2. **分组求和** ```java Map<String, Integer> sumMap = dataList.stream() .collect(Collectors.groupingBy( item -> (String) item.get("category"), Collectors.summingInt(item -> (Integer) item.get("value")) )); ``` --- ### **四、多字段组合分组** 若需要按多个字段组合统计(如`category`+`status`): ```java Map<List<Object>, Long> multiFieldCount = dataList.stream() .collect(Collectors.groupingBy( item -> Arrays.asList( item.get("category"), item.get("status") ), Collectors.counting() )); ``` --- ### **五、注意事项** 1. **类型安全**:从`Object`取值时需显式强制类型转换,确保类型匹配 2. **空值处理**:建议使用`getOrDefault()`或过滤空值: ```java .filter(item -> item.get("category") != null) ``` 3. **复杂统计**:可使用`Collectors.summarizingInt()`获取统计摘要(总和/平均值/最大值等) --- ### **六、完整示例** ```java import java.util.*; import java.util.stream.*; public class ListMapAggregation { public static void main(String[] args) { List<Map<String, Object>> dataList = new ArrayList<>(); dataList.add(Map.of("category", "A", "value", 10)); dataList.add(Map.of("category", "B", "value", 20)); dataList.add(Map.of("category", "A", "value", 5)); // 分组计数 Map<String, Long> countResult = dataList.stream() .collect(Collectors.groupingBy( e -> (String) e.get("category"), Collectors.counting() )); System.out.println("计数结果:" + countResult); // {A=2, B=1} // 分组求和 Map<String, Integer> sumResult = dataList.stream() .collect(Collectors.groupingBy( e -> (String) e.get("category"), Collectors.summingInt(e -> (Integer) e.get("value")) )); System.out.println("求和结果:" + sumResult); // {A=15, B=20} } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值