河南省第二届ACM程序设计大赛——第二题 奇特的艺术品
- Code:
- #include<iostream>
- #include<stdlib.h>
- using namespace std;
- typedef struct Lnode{
- int data;
- struct Lnode *next;
- }Lnode, *Linklist;
- void Initlist(Linklist &L)
- {
- L=new Lnode;
- }
- void Destroy(Linklist &L)
- {
- Linklist p;
- p=L;
- while(p)
- {
- p=p->next;
- delete L;
- L=p;
- }
- }
- int main()
- {
- Linklist L,p,q;
- Linklist B,H,T;//B记录要断开的位置,H,T,记录断开片段首尾
- Initlist (L);
- p=L;
- int n,k;
- int a,b,c;
- int i,j;
- cin >> n >> k;
- for(i=1;i<=n;i++)
- {
- Initlist (q);
- q->data=i;
- p->next=q;
- p=q;
- }
- p->next=NULL;
- for(i=0;i<k;i++)
- {
- cin >> a >> b >> c;
- for(j=1,p=L;j<=b;j++)//查找断开位置
- {
- if(j==a){B=p;H=p->next;}
- p=p->next;
- }
- T=p;
- B->next=T->next;
- if(!c)//c=0插入顶端
- {
- T->next=L->next;
- L->next=H;
- }
- else{//查找插入位置
- p=L;
- for(j=0;j<c;j++)
- {
- p=p->next;
- }
- T->next=p->next;
- p->next=H;
- }
- }
- p=L->next;
- for(i=0;i<10;i++)
- {
- cout << p->data << endl;
- p=p->next;
- }
- Destroy(L);
- system("pause");
- return 0;
- }