• 题目四 艺术品


    Dr.Kong设计了一件艺术品,该艺术品由N个构件堆叠而成,N个构件从高到低按层编号依次为1,2,……,N。艺术品展出后,引起了强烈的反映。Dr.Kong观察到,人们尤其对作品的高端部分评价甚多。

    狂热的Dr.Kong一激动,对组成该艺术品的N个构件重新组合,比如:把第6层到第12层的构件搬下来,想一想,然后整体放到剩下构件的第7层下面;过一会儿,又把第2层到第9层的构件搬下来,整体放到剩下构件的第1层下面等等。于是,Dr.Kong在进行了连续若干次“搬来搬去”后,还是这N个构件,又诞生了一件新的艺术品。

    编程:请输出新的艺术品最高十层构件的编号。

    【标准输入】

    第一行: N K       表示构件的总数和“搬来搬去”的总次数

    第2~K+1行:A B C      表示要搬动的构件(即从第A层到第B层)整个放在第C层下面;

    如果C等于0,则要搬动的构件将放到最高层。

    【标准输出】

    由十行组成,分别为组成新艺术品的第一层到第十层构件的编号。

    【约束条件】

    (1)   10≤N≤20000    1≤k≤1000

    (2)   1≤A≤B≤N,      0≤C≤N-(B-A+1)

    【 样  例 】

    标准输入

    标准输出

    13 3

    6 12 1

    2 9 0

    10 13 8

     

    6

    7

    8

    9

    10

    11

    12

    2

    3

    4

      数据结构的作业,我用的方法算是很笨的了,并且我也没经历过算法训练,所以代码写的很差,仅供参考。

      整体思路就是用链表保存艺术品,节点定义为:

      

    typedef struct Lnode{ 
    	int num;
    	Lnode* next;
    }node,*LinkList;
    

      num表示艺术品编号。

      整体代码如下:

      1 /*
      2 zp
      3 zzti
      4 2012/10/06
      5 */
      6 #include<iostream>
      7 using namespace std;
      8 typedef struct Lnode{
      9     int num;  //艺术品编号
     10     Lnode* next;
     11 }node,*LinkList;
     12 void CreatList(LinkList& l,int n)  //初始化链表,给所有节点赋值
     13 {
     14     node* p=new node;
     15     p->next=NULL;
     16     p->num=0;
     17     l=p;
     18     for(int i=0;i<n;i++)
     19     {
     20         node* q=new node;
     21         q->num=i+1;
     22         q->next=NULL;
     23         p->next=q;
     24         p=p->next;
     25     }
     26 }
     27 
     28 void Move(LinkList& l,int a ,int b,int c){
     29     node* p=l; 
     30     node* headpre,*tailpre;  //a的前一个位置和b的前一个位置
     31     int i=0;
     32     //第一次遍历,找出ab的位置
     33     while(p->next!=NULL)
     34     {
     35         i++;
     36         if(i==a)
     37         {
     38             headpre=p;   
     39         }
     40         if(i==b)
     41         {
     42             tailpre=p;;
     43         }
     44         p=p->next;
     45     }
     46     node* head=headpre->next; //a的位置
     47     node* tail=tailpre->next;  //b的位置
     48     headpre->next=tail->next;
     49 
     50     //第二次遍历,将抽出的链表重新插入到合适的位置
     51     p=l;i=0;
     52     while(p->next)
     53     {
     54         if(i==c)
     55         {
     56             node* q=p->next;  //将链表插入
     57             p->next=head;
     58             tail->next=q;
     59         }
     60         i++;p=p->next;
     61     }
     62 }
     63 
     64 void PrRes(LinkList l) { //PrintResult
     65     int i=0;
     66     node* p=l->next;
     67     while(i<10)
     68     {
     69         cout<<p->num<<endl;
     70         i++;
     71         p=p->next;
     72     }
     73 }
     74 
     75 
     76 int main()
     77 {
     78     
     79     int n,k,a,b,c,t1,t2,t3;
     80     cout<<"请输入n,k:"<<endl;
     81     do{
     82         cin>>t1>>t2;
     83         if(t1<10||t1>20000||t2<1||t2>>1000)
     84         {
     85             cout<<"输入数据超过规定范围,请重新输入!"<<endl;
     86         }
     87         else break;
     88         
     89     }while(1);
     90     n=t1;k=t2;
     91     LinkList l;
     92     CreatList(l,n);
     93     for(int i=0;i<k;i++)
     94     {
     95         cout<<"请输入要搬运的范围以及放置位置:"<<endl;
     96         do{
     97             cin>>t1>>t2>>t3;
     98             if(t1<1||t1>n||t2<1||t2>n||t1>t2||t3<0||t3>(n-(t2-t1+1)))
     99             {
    100                 cout<<"输入数据有误,请重新输入!"<<endl;
    101             }
    102             else break;
    103         }while(1);
    104         a=t1,b=t2,c=t3;
    105         Move(l,a,b,c);
    106     }
    107     PrRes(l);
    108     return 0;
    109 }
    110     
  • 相关阅读:
    python之enumerate
    Python中的集合set
    SGU 分类
    太空飞行计划 最大权闭合图
    1.飞行员配对 二分图匹配(输出方案)/最大流斩
    poj1149最大流经典构图神题
    hdu1569 方格取数 求最大点权独立集
    最大独立集 最小点覆盖 最小边覆盖 最小路径覆盖 最大团
    hdu3491最小割转最大流+拆点
    hdu3987,最小割时求最少割边数
  • 原文地址:https://www.cnblogs.com/csonezp/p/2712685.html
Copyright © 2020-2023  润新知