POJ 1978 Hanafuda Shuffle

一叠卡片从上到下编号一次为 n, n-1, ..., 3, 2, 1,每次从这一叠卡片中抽出若干张放到最上面(如图),经过若干次这样的操作后,问最顶层的卡片的编号是多少。


直接用静态链表来模拟移动的操作然后输出表头元素。

Code:

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 55;
struct node
{
    int d;
    node* next;
};
node cards[maxn];
node* head;
void makelist( int n )
{
    node* cur;
    node* tp;
    tp = new node;
    tp->d = -1;
    tp->next = NULL;
    head = tp;
    cur = head;
    for( int i = 1; i <= n; i++ )
    {
        tp = new node;
        tp->d = n-i+1;
        tp->next = NULL;
        cur->next = tp;
        cur = tp;
    }
}
void cutting( int p, int c )
{
    node* st;
    node* ed;
    node* lk;
    st = head;
    lk = head;
    for( int i = 0; i < p; i++ )
    {
        st = st->next;
        if( i )
            lk = lk->next;
    }
    ed = st;
    for( int i = 0; i < c-1; i++ )
        ed = ed->next;
    lk->next = ed->next;
    ed->next = head->next;
    head->next = st;
}
int main()
{
    int n, r;
    while( scanf( "%d%d", &n, &r ), n + r )
    {
        makelist( n );
        int p, c;
        for( int i = 0; i < r; i++ )
        {
            scanf( "%d%d", &p, &c );
            cutting( p, c );
        }
        printf( "%d\n", head->next->d );
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值