生活随笔
收集整理的這篇文章主要介紹了
sdutoj-4209-移动小球
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Description 給你n個(gè)小球,從左到右編號(hào)依次為1,2,3,4,5,6…n排成一行。現(xiàn)在有以下2種操作:A x y表示把編號(hào)為x小球移動(dòng)到編號(hào)為y的小球的左邊(和y相鄰)。Q x為詢(xún)問(wèn)編號(hào)為x的小球左邊的球號(hào),如果x左邊沒(méi)有小球的話(huà)輸出"cyk666"。
Input 第一行輸入一個(gè)T,表示有T組測(cè)試數(shù)據(jù)。(1<=T<=100)
隨后每一組測(cè)試數(shù)據(jù)第一行是兩個(gè)整數(shù)N,M,其中N表示球的個(gè)數(shù)(1 隨后有M行詢(xún)問(wèn),第一個(gè)字符是操作類(lèi)型s。
當(dāng)s為’A’時(shí),輸入x,y表示把編號(hào)為x小球移動(dòng)到編號(hào)為y的小球的左邊。
當(dāng)s為’Q’時(shí),輸入x表示詢(xún)問(wèn)小球x左邊的球號(hào)。保證(1<=x<=N,1<=y<=N,1<=N<=100000)
Output 輸出每次詢(xún)問(wèn)的球號(hào),如果這樣的小球不存在,輸出"cyk666"(不包括引號(hào))。
Sample Input 1 6 5 A 1 2 A 1 4 A 3 5 Q 5 Q 2 Output 3 cyk666
#include <bits/stdc++.h> using namespace std
; typedef struct node
{ int data
; struct node
* next
;
} List
;
List
* creat ( int n
)
{ List
* head
, * tail
, * p
; head
= new List
; head
- > next
= NULL ; tail
= head
; for ( int i
= 1 ; i
<= n
; i
++ ) { p
= new List
; p
- > data
= i
; p
- > next
= NULL ; tail
- > next
= p
; tail
= p
; } return head
;
}
void move_A ( List
* head
, int x
, int y
)
{ List
* t
, * p
; t
= head
; p
= head
- > next
; while ( p
- > data
!= x
) { p
= p
- > next
; t
= t
- > next
; } t
- > next
= p
- > next
; free ( p
) ; p
= new List
; p
- > next
= NULL ; p
- > data
= x
; t
= head
; while ( t
- > next
- > data
!= y
) t
= t
- > next
; p
- > next
= t
- > next
; t
- > next
= p
;
}
void display_Q ( List
* head
, int x
)
{ List
* p
; p
= head
; while ( p
- > next
- > data
!= x
&& p
) p
= p
- > next
; if ( p
== head
|| p
== NULL ) printf ( "cyk666\n" ) ; else printf ( "%d\n" , p
- > data
) ;
}
int main ( )
{ List
* head
; int T
, n
, m
, x
, y
; char s
; scanf ( "%d" , & T
) ; while ( T
-- ) { scanf ( "%d%d" , & n
, & m
) ; head
= creat ( n
) ; while ( m
-- ) { getchar ( ) ; scanf ( "%c" , & s
) ; if ( s
== 'A' ) { scanf ( "%d%d" , & x
, & y
) ; move_A ( head
, x
, y
) ; } if ( s
== 'Q' ) { scanf ( "%d" , & x
) ; display_Q ( head
, x
) ; } } } return 0 ;
}
總結(jié)
以上是生活随笔 為你收集整理的sdutoj-4209-移动小球 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。