Shuffle Card

本文介绍了一种卡片洗牌算法,通过将特定编号的卡片放置到队列的首位,重复此过程m次后,输出最终的卡片顺序。文章提供了一个C语言实现的例子,展示了如何通过数组记录每张卡片的位置,并在完成所有操作后输出卡片的新顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
A deck of card consists of n cards. Each card is different, numbered from 1 to n. At first, the cards were ordered from 1 to n. We complete the shuffle process in the following way, In each operation, we will draw a card and put it in the position of the first card, and repeat this operation for m times.

Please output the order of cards after m operations

Input
The first line of input contains two positive integers n and m.(1<=n,m<=105)

The second line of the input file has n Numbers, a sequence of 1 through n.

Next there are m rows, each of which has a positive integer si, representing the card number extracted by the i-th operation.

Output
Please output the order of cards after m operations. (There should be one space after each number.)

Sample Input

5 3
1 2 3 4 5
3
4
3

Sample Output

3 4 1 2 5

题意描述:
给出n个数是1到n然后再给出m个数,每一个数代表一种操作,就是把这个数放在队首,然后在输出,这m中操作结束后的数组中元素的顺序。

#include<string.h>
#include<stdio.h>

int book[100100],s[300100];
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    int i,j,k,a,b;
    for(i=1;i<=m;i++)
        book[i]=0;
    for(i=100100;i<100100+m;i++)
        scanf("%d",&s[i]);
    for(i=100099;i>100099-n;i--)
        scanf("%d",&s[i]);
    printf("%d ",s[100100-n]);
    book[s[100100-n]]=1;
    for(i=100100-n+1;i<100100+m;i++)
    {
        if(book[s[i]]==0)
        {
            printf("%d ",s[i]);
            book[s[i]]=1;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值