计数问题+约瑟夫问题(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;
    }
}
相关推荐
666HZ66621 分钟前
数据结构2.0 线性表
c语言·数据结构·算法
SmartRadio27 分钟前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
余瑜鱼鱼鱼1 小时前
Java数据结构:从入门到精通(十二)
数据结构
实心儿儿1 小时前
Linux —— 基础开发工具5
linux·运维·算法
charlie1145141912 小时前
嵌入式的现代C++教程——constexpr与设计技巧
开发语言·c++·笔记·单片机·学习·算法·嵌入式
清木铎3 小时前
leetcode_day4_筑基期_《绝境求生》
算法
清木铎3 小时前
leetcode_day10_筑基期_《绝境求生》
算法
j_jiajia3 小时前
(一)人工智能算法之监督学习——KNN
人工智能·学习·算法
源代码•宸4 小时前
Golang语法进阶(协程池、反射)
开发语言·经验分享·后端·算法·golang·反射·协程池
CSDN_RTKLIB5 小时前
【字符编码】有无BOM的UTF-8
c++