5 条题解
-
1端木俁 (房佳坤) LV 10 @ 2021-04-21 20:47:04
感觉list和vector差不多
#include<iostream> #include<list> using namespace std; int main() { list<int> a; int n,k; cin>>n>>k; int cnt = 1; for(int i=1; i<=n; i++) a.push_back(i); list<int>::iterator it=a.begin(); for(; a.size()>1;){ if(cnt==k){ cnt=1; list<int>::iterator it1=it; it++; if(it==a.end()) it=a.begin(); a.erase(it1); } else{ cnt++,it++; if(it==a.end()) it=a.begin(); } } list<int>::iterator f=a.begin(); cout<<*f<<" "; return 0; }
-
02023-11-21 15:45:25@
#include <stdio.h> int main() { int a[10001] = {0}; // 初始化数组作为环 int n, m; // n代表总的人数,m代表报数到几退出 scanf("%d %d", &n, &m); int count = 0; // 记录退出的个数 int k = -1; // 这里假定开始为第一个人,下标为0,编号为1,如需从编号x开始,则k=x-2 while (count < n - 1) { // 总共需要退出n-1个人 int i = 0; // 记录当前报数编号 while (i < m) { k = (k + 1) % n; // 循环处理下标 if (a[k] == 0) { i++; if (i == m) { a[k] = -1; count++; } } } } for (int i = 0; i < n; i++) { if (a[i] == 0) { printf("%d\n", i + 1); break; } } return 0; }
-
02021-04-13 19:12:41@
#include<bits/stdc++.h>
using namespace std;
bool a[1000001];
int main() {
int n,m,L=0;
cin>>n>>m;
for(int i=1; i<=n; ++i)
a[i]==false;
int f=0,t=0,s=0;
do {
++t;
if(t==n+1) t=1;
if(a[t]==false) ++s;
if(s==m) {
s=0;
a[t]=true;
L=t;
++f;
}
}
while(f!=n);
cout<<L;
return 0;
} -
-22021-02-09 14:27:57@
#include<iostream>
#include<vector>
using namespace std;
template <typename T>
int Joseph(int n,int k)
{
T a;
for(int i=1;i<=n;i++) a.push_back(i);
int start = 0;while(a.size()!=1)
{
start = (start+k-1)%a.size();
a.erase(a.begin()+(start));
}
return a[0];
}
int main()
{
int n,k;
cin>>n>>k;
int ans = Joseph<vector<int>>(n,k);
cout << ans << endl;
return 0;
} -
-32020-05-26 10:49:06@
#include<iostream> #include<vector> #include<iterator> using std::vector; using std::iterator; using std::cout; using std::cin; int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < arr.size(); i++) arr[i] = i+1; int k; cin >> k; vector<int> out; for (vector<int>::iterator i = arr.begin(); !arr.empty(); ) { int loc = i - arr.begin() + k - 1; while (loc >= arr.size()) loc = loc - arr.size(); i = arr.begin() + loc; out.push_back(*i); i = arr.erase(i); } cout << out[out.size() - 1]; return 0; }
- 1
信息
- 难度
- 2
- 分类
- (无)
- 标签
- 递交数
- 1542
- 已通过
- 370
- 通过率
- 24%
- 被复制
- 10
- 上传者