目录
一、计数问题
二、约瑟夫问题
一、计数问题
#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;}
}