当前位置: 首页 > article >正文

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

目录

一、计数问题

 二、约瑟夫问题


一、计数问题

#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;
}

 二、约瑟夫问题

#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;
    }
}

 


http://www.kler.cn/a/144462.html

相关文章:

  • 手写模拟Spring底层原理(Spring启动流程)(思维导图)
  • SQL 注入漏洞的理解
  • Python爬虫知识储备
  • AT89S52单片机智能寻迹小车自动红外避障趋光检测发声发光设计
  • 网安融合新进展:Check Point+七云网络联合研发,加固大型企业边缘、分支侧安全
  • Linux中安装MySQ-合集
  • 微服务实战系列之签名Sign
  • 网络运维与网络安全 学习笔记2023.11.22
  • 【计算机网络】(网络层)定长掩码和变长掩码
  • 『亚马逊云科技产品测评』活动征文|EC2 实例安装 docker 与配套软件部署前后端分离的医疗管理后台系统
  • spark数据倾斜的解决思路
  • 【Range Image】 创建Range Image
  • 05_使用API_Arrays与Lambda
  • 发送一个网络数据包的过程解析
  • Unity中Shader的Standard材质解析(一)
  • 开发者的 Debian 12 KDE 配置优化指南
  • 聚类笔记:HDBSCAN
  • 【ARM CoreLink 系列 3.1 -- CCI-500 详细介绍 -上半部】
  • 从裸机启动开始运行一个C++程序(十三)
  • 【自主探索】基于 rrt_exploration 的单个机器人自主探索建图