CDOJ 猴子选大王

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值