计数问题+约瑟夫问题(map)

目录

一、计数问题

二、约瑟夫问题


一、计数问题

cpp 复制代码
#include<iostream>
#include<map>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    map<int,int>m;
    for(int i=1;i<=n;i++)
    {
        if(i>=1 && i<10)
        {
            m[i]++;
        }
        else
        {
           int temp = i;
            while (temp > 0)
            {
                int digit = temp % 10;
                m[digit]++;
                temp /= 10;
            }
        }
    }
    
    cout<<m[x];
    return 0;
}

二、约瑟夫问题

cpp 复制代码
#include<iostream>
#include<map>
using namespace std;
int main()
{
    int n, k, m;
    cin >> n >> k >> m;
    map<int, int>_map;
    for (int i = 0; i < n; i++)
    {
        _map[i]++;
    }

    int start = k;
    int count = 0;
    int total = n;
    while (total != 1)
    {
        if (_map[start] == 1)
        {
            count++;
            if (count == m)
            {
                _map[start] = 0;
                --total;
                count = 0;
            }
        }
        start = (start + 1) % n;
    }

    map<int, int>::iterator it = _map.begin();
    while (it != _map.end())
    {
        if (it->second == 1)
        {
            cout << it->first;
            return 0;
        }
        ++it;
    }
}
相关推荐
FakeOccupational2 小时前
【数学 密码学】量子通信:光的偏振&极化的量子不确定性特性 + 量子密钥分发 BB84算法步骤
算法·密码学
ZhengEnCi4 小时前
S10-蓝桥杯 17822 乐乐的积木塔
算法
贾斯汀玛尔斯4 小时前
每天学一个算法--拓扑排序(Topological Sort)
算法·深度优先
t***5444 小时前
如何配置Orwell Dev-C++使用Clang
开发语言·c++
大龄程序员狗哥4 小时前
第25篇:Q-Learning算法解析——强化学习中的经典“价值”学习(原理解析)
人工智能·学习·算法
exp_add34 小时前
质数相关知识
算法
CoderCodingNo4 小时前
【信奥业余科普】C++ 的奇妙之旅 | 13:为什么 0.1+0.2≠0.3?——解密“爆int”溢出与浮点数精度的底层原理
开发语言·c++
小辉同志5 小时前
215. 数组中的第K个最大元素
数据结构·算法·leetcode··快速选择
小O的算法实验室6 小时前
2025年IEEE TITS,基于矩阵的进化计算+面向无线传感器网络数据收集无人机路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
OidEncoder6 小时前
编码器分辨率与机械精度的关系
人工智能·算法·机器人·自动化