计数问题+约瑟夫问题(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;
    }
}
相关推荐
总斯霖几秒前
HDU 4857 - 逃生 题解
算法
-To be number.wan1 分钟前
算法学习日记 | 模拟
c++·学习·算法
EQ-雪梨蛋花汤2 分钟前
【问题反馈】JNI 开发:为什么 C++ 在 Debug 正常,Release 却返回 NaN?
开发语言·c++
Blossom.1182 分钟前
从“金鱼记忆“到“超级大脑“:2025年AI智能体记忆机制与MoE架构的融合革命
人工智能·python·算法·架构·自动化·whisper·哈希算法
王老师青少年编程2 分钟前
2023信奥赛C++提高组csp-s复赛真题及题解:密码锁
c++·真题·csp·密码锁·信奥赛·csp-s·提高组
金枪不摆鳍3 分钟前
算法-贪心算法
算法·贪心算法
naruto_lnq3 分钟前
高性能消息队列实现
开发语言·c++·算法
charlie1145141913 分钟前
malloc 在多线程下为什么慢?——从原理到实测
开发语言·c++·笔记·学习·工程实践
池央5 分钟前
贪心算法-摆动序列
算法·贪心算法
D_evil__5 分钟前
【Effective Modern C++】第四章 智能指针:18. 使用独占指针管理具备专属所有权的资源
c++