计数问题+约瑟夫问题(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;
    }
}
相关推荐
啊森要自信2 小时前
【QT】常⽤控件详解(四)常用显示类控件类 Label && LCDNumber && ProgressBar && Calendar Widget
开发语言·数据库·c++·qt·qt6.3
好易学·数据结构3 小时前
可视化图解算法57:字符串的排列
数据结构·算法·leetcode·面试·笔试·回溯算法·牛客
এ᭄画画的北北5 小时前
力扣-283.移动零
算法·leetcode
幽迷狂6 小时前
AFSIM入门教程03.03:更新所有依赖库版本
c++·qt·仿真·osgearth·osg·军事·afsim
勇闯逆流河6 小时前
【C++】Stack and Queue and Functor
开发语言·c++
2501_924879368 小时前
口罩识别场景误报率↓79%:陌讯多模态融合算法实战解析
人工智能·深度学习·算法·目标检测·智慧城市
Christo38 小时前
TFS-2022《A Novel Data-Driven Approach to Autonomous Fuzzy Clustering》
人工智能·算法·机器学习·支持向量机·tfs
木木子99998 小时前
超平面(Hyperplane)是什么?
算法·机器学习·支持向量机·超平面·hyperplane
TravisBytes9 小时前
gRPC C++ 从 0 到 1 → 到线上:**超详细** 环境搭建、编码范式、性能调优与 DevOps 全攻略
开发语言·c++·devops
星空下的曙光10 小时前
React 虚拟 DOM Diff 算法详解,Vue、Snabbdom 与 React 算法对比
vue.js·算法·react.js