http://www.acm.uestc.edu.cn/#/problem/show/525
分析:
模拟题,顺着题意做就好
下面给出两种解决方法
1:(动态)链表
2:(静态链表)数组
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
typedef struct _monkey
{
int number;
struct _monkey *next;
} monkey;
int main()
{
//freopen("int.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d", &T);
while(T--)
{
int m, n;
monkey *pre, *last, *newnode;
scanf("%d %d", &m, &n);
pre = (monkey *)malloc(sizeof(monkey));
pre -> number = 1;
last = pre;
for(int i = 2; i <= m; i++)
{
newnode = (monkey *)malloc(sizeof(monkey));
newnode -> number = i;
last -> next = newnode;
last = newnode;
}
last -> next = pre;//循环链表建立完毕
for(int i = 0; i < m - 1; i++)//每次退出一次猴子
{
for(int t = 1;t < n;t++)
{
last = pre;
pre = pre -> next;
}
last -> next = pre -> next;
free(pre);
pre = last -> next;
}
printf("%d\n", pre -> number);
free(pre);
}
return 0;
}
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
typedef struct _monkey
{
int number;
struct _monkey *next;
} monkey;
#define N 103
int main()
{
//freopen("int.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d", &T);
while(T--)
{
int m, n;
int last, pre, monkey[N];
scanf("%d %d", &m, &n);
//首先考虑猴子的编号为0 ~ m-1,输出时+1即可
for(int i = 0; i < m; i++) //形成静态链表
monkey[i] = (i + 1) % m;
last = m - 1;
pre = 0; //第一次报数猴子的下标
for(int i = 0; i < m - 1; i++) //每次退出一个
{
for(int t = 1;t < n;t++)
{
last = pre; //保留上一个的下标
pre = monkey[pre]; //得到下一个猴子的下标
}
monkey[last] = monkey[pre]; //改变链接,退出,
pre = monkey[pre]; //下一次第一个报数的猴子下标
}
printf("%d\n", pre + 1);
}
return 0;
}