计数问题+约瑟夫问题(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;
    }
}
相关推荐
变量未定义~3 分钟前
字符串哈希匹配字符串
数据结构·算法·哈希算法
lzh200409193 分钟前
手搓一个简易 Linux 进程池:巩固进程知识
linux·c++
周末也要写八哥6 分钟前
浅谈二叉树的深度优先搜索(DFS)算法
算法·深度优先
basketball61610 分钟前
C++ 的 const 相关知识点总结
开发语言·c++
y = xⁿ18 分钟前
20天速通LeetCodeday17:一维动态规划
算法
bnmoel22 分钟前
数据结构深度剖析栈与队列:结构、边界实现与进出操作全解析
c语言·数据结构·算法··队列
WL_Aurora24 分钟前
Python 算法基础篇之查找算法(一):顺序查找、二分查找与插值查找
开发语言·python·算法
阿文的代码库30 分钟前
对于C++中push_back的原理介绍与分析
开发语言·c++
ChoSeitaku34 分钟前
06_可变参数_递归_类和对象_封装
java·数据结构·算法
枕星而眠34 分钟前
C++ 核心语法精讲:auto / 模板 / 命名空间 / 动态内存 从用法到面试
开发语言·c++·面试