奇特的艺术品

来源:互联网 发布:淘宝开店版权怎么写 编辑:程序博客网 时间:2024/03/29 09:43

河南省第二届ACM程序设计大赛——第二题 奇特的艺术品

Code:
  1.   
  2. Code:   
  3.  #include<iostream>      
  4. #include<stdlib.h>      
  5. using namespace std;      
  6.      
  7. typedef struct Lnode{      
  8. int data;      
  9. struct Lnode *next;      
  10. }Lnode, *Linklist;      
  11.      
  12. void Initlist(Linklist &L)      
  13. {      
  14.     L=new Lnode;      
  15. }      
  16. void Destroy(Linklist &L)      
  17. {      
  18.     Linklist p;      
  19.     p=L;      
  20.     while(p)      
  21.     {      
  22.         p=p->next;      
  23.         delete L;      
  24.         L=p;      
  25.     }      
  26. }      
  27. int main()      
  28. {      
  29.     Linklist L,p,q;      
  30.     Linklist B,H,T;//B记录要断开的位置,H,T,记录断开片段首尾      
  31.     Initlist (L);      
  32.     p=L;      
  33.     int n,k;      
  34.     int a,b,c;      
  35.     int i,j;      
  36.      
  37.     cin >> n >> k;      
  38.     for(i=1;i<=n;i++)      
  39.     {      
  40.         Initlist (q);      
  41.         q->data=i;      
  42.         p->next=q;      
  43.         p=q;      
  44.     }      
  45.     p->next=NULL;      
  46.     for(i=0;i<k;i++)      
  47.     {      
  48.         cin >> a >> b >> c;      
  49.         for(j=1,p=L;j<=b;j++)//查找断开位置      
  50.         {      
  51.             if(j==a){B=p;H=p->next;}      
  52.             p=p->next;      
  53.         }      
  54.         T=p;      
  55.         B->next=T->next;      
  56.         if(!c)//c=0插入顶端      
  57.         {      
  58.         T->next=L->next;      
  59.         L->next=H;      
  60.         }      
  61.         else{//查找插入位置      
  62.             p=L;      
  63.             for(j=0;j<c;j++)      
  64.             {      
  65.                 p=p->next;      
  66.             }      
  67.             T->next=p->next;      
  68.             p->next=H;      
  69.         }      
  70.     }      
  71.     p=L->next;      
  72.     for(i=0;i<10;i++)      
  73.     {      
  74.         cout << p->data << endl;      
  75.         p=p->next;      
  76.     }      
  77. Destroy(L);      
  78.     system("pause");      
  79.     return 0;      
  80. }      
  81.