K - 移动小球

Description
给你n个小球,从左到右编号依次为1,2,3,4,5,6…n排成一行。现在有以下2种操作:A x y表示把编号为x小球移动到编号为y的小球的左边(和y相邻)。Q x为询问编号为x的小球左边的球号,如果x左边没有小球的话输出"cyk666"。

Input
第一行输入一个T,表示有T组测试数据。(1<=T<=100)

随后每一组测试数据第一行是两个整数N,M,其中N表示球的个数(1 随后有M行询问,第一个字符是操作类型s。

当s为’A’时,输入x,y表示把编号为x小球移动到编号为y的小球的左边。

当s为’Q’时,输入x表示询问小球x左边的球号。保证(1<=x<=N,1<=y<=N,1<=N<=100000)

Output
输出每次询问的球号,如果这样的小球不存在,输出"cyk666"(不包括引号)。

Sample

Input 
1
6 5
A 1 2
A 1 4
A 3 5
Q 5
Q 2
Output 
3
cyk666
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

typedef struct node
{
    int date;
    struct node *next;
} Node;

int a,b;

//
void Ad_just(Node *head,int a,int b)
{
    int t;
    Node *q,*q1,*p,*pnew;
    //删去结点,并记录数值。
    q=head;
    q1=head->next;
    while(q->next->date!=a)
    {
        q=q->next;
        q1=q1->next;
    }
    t=q->next->date;
    q->next=q1->next;
    free(q1);
    
    //插入结点,并赋值
    p=head;
    while(p->next->date!=b)
    {
        p=p->next;
    }
    pnew=(Node *)malloc(sizeof(Node));
    pnew->date=t;
    pnew->next=p->next;
    p->next=pnew;
}

void Find(Node *head,int a)
{
    Node *p;
    //查找结点。
    p=head;
    while(p->next->date!=a)
    {
        p=p->next;
    }

    if(p==head||p==NULL)
    {
        printf("cyk666\n");
    }
    else
    {
        printf("%d\n",p->date);
    }
}

Node *creat(int n)
{
    Node *head,*p,*tail;
    head=(Node *)malloc(sizeof(Node));
    head->next=NULL;
    tail=head;
    for(int i=1; i<=n; i++)
    {
        p=(Node *)malloc(sizeof(Node));
        p->date=i;
        tail->next=p;
        tail=p;
        p->next=NULL;
    }
    return head;
}

int main()
{
    int T,n,m,x,y;
    Node *head;
    char s;
    cin>>T;
    while(T--)
    {
        scanf("%d %d",&n,&m);
        head=creat(n);
        for(int i=1; i<=m; i++)
        {
            cin>>s;
            if(s=='A')
            {
                scanf("%d %d",&x,&y);
                Ad_just(head,x,y);
            }
            else
            {
                scanf("%d",&x);
                Find(head,x);
            }
        }
    }
    return 0;
}

开始做错了,做成了以链表下标来经行插入和查找。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

typedef struct node
{
    int date;
    struct node *next;
} Node;

int a,b;

void Output(Node *head)
{
    Node *p;
    p=head->next;
    while(p)
    {
        printf("%d%c",p->date,p->next!=NULL?' ':'\n');
        p=p->next;
    }
}

void Swap(Node *head,int a,int b)
{
    int t;
    Node *p,*q,*q1,*newnode;
    q=head,q1=head->next;
    a=a-1;
    while(a--)
    {
        q=q->next;
        q1=q1->next;
    }
    t=q->next->date;
    q->next=q1->next;
    free(q1);

    Output(head);

    p=head;
    b=b-2;
    while(b--)
    {
        p=p->next;
    }
    newnode=(Node *)malloc(sizeof(Node));
    newnode->date=t;
    newnode->next=p->next;
    p->next=newnode;
    Output(head);
}

void Find(Node *head,int a)
{
    Node *p;
    p=head;
    a=a-1;
    while(a--)
    {
        p=p->next;
    }

    if(p==head->next||p==NULL)
    {
        printf("cyk666\n");
    }
    else
    {
        printf("%d\n",p->date);
    }
}

Node *creat(int n)
{
    Node *head,*p,*tail;
    head=(Node *)malloc(sizeof(Node));
    head->next=NULL;
    tail=head;
    for(int i=1; i<=n; i++)
    {
        p=(Node *)malloc(sizeof(Node));
        p->date=i;
        tail->next=p;
        tail=p;
        p->next=NULL;
    }
    return head;
}
int main()
{
    int T,n,m;
    Node *head;
    char s;
    cin>>T;
    while(T--)
    {
        scanf("%d %d",&n,&m);
        head=creat(n);

        for(int i=1; i<=m; i++)
        {
            cin>>s;
            int x,y;
            if(s=='A')
            {
                scanf("%d %d",&x,&y);
                Swap(head,x,y);
            }
            else
            {
                scanf("%d",&x);
                Find(head,x);
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码敲上天.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值