zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practise

     http://www.patest.cn/contests/pat-a-practise/1098

    According to Wikipedia:

    Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

    Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

    Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (<=100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

    Sample Input 1:

    10
    3 1 2 8 7 5 9 4 6 0
    1 2 3 7 8 5 9 4 6 0
    

    Sample Output 1:

    Insertion Sort
    1 2 3 5 7 8 9 4 6 0
    

    Sample Input 2:

    10
    3 1 2 8 7 5 9 4 6 0
    6 4 5 1 0 3 2 7 8 9
    

    Sample Output 2:

    Heap Sort
    5 4 3 1 0 2 6 7 8 9



    此题是2015年春季的研究生入学考试复试时的机试题,链接 http://www.patest.cn/contests/graduate-entrance-exam-2015-03-20
     1 #include<cstdio>
     2 #include<cstring> 
     3 
     4 void headshift(int *a,int i,int len)//数组  要处理的节点下标  当前处理的堆的长度 
     5 {
     6      if(i>=len/2 || i<0) return;//叶节点可看作一个满足性质的堆,所以只对非叶子结点进行处理即可 
     7      int max=i; //调整一个节点为(节点值、左孩子节点值、右孩子节点值)三者中的最大值 
     8      if(2*i+1<=len-1 && a[max]<a[2*i+1]) max=2*i+1;
     9      if(2*i+2<=len-1 && a[max]<a[2*i+2]) max=2*i+2;
    10      if(max!=i) 
    11      {
    12          int temp=a[max];
    13          a[max]=a[i],a[i]=temp;
    14          headshift(a,max,len);//某节点与其某个孩子节点的数值交换调整后,可能会影响该孩子节点的堆的性质,所以要向下调整 
    15      }
    16      return;
    17 }
    18 
    19 int match(int *temp,int *a,int len)
    20 {
    21     for(int i=0;i<len;i++)
    22         if(temp[i]!=a[i]) return 0;   
    23     return 1;
    24 }
    25 
    26 void print(int *a,int len)
    27 {     
    28      for(int i=0;i<len;i++)
    29      {
    30              if(i) printf(" %d",a[i]);
    31              else printf("%d",a[i]);
    32      }
    33      return;
    34 }
    35 
    36 void Insertion(int *a,int i)
    37 {
    38      int loc=i-1,temp=a[i];
    39      for(;loc>=0;loc--) if(a[loc]<=temp) break;//寻找应该插入的位置 
    40      loc++;
    41      if(loc!=i) for(int j=i;j>loc;j--) a[j]=a[j-1];//比当前处理值大的元素相应后移 
    42      a[loc]=temp;
    43 }
    44 int main()
    45 {
    46     
    47    int num=0,data[100]={0},sorttemp[100]={0},a[100]={0};
    48    scanf("%d",&num);
    49    for(int i=0;i<num;i++) scanf("%d",data+i);
    50    for(int i=0;i<num;i++) scanf("%d",sorttemp+i);   
    51    
    52    for(int i=0;i<num;i++) a[i]=data[i];
    53    for(int i=1;i<num;i++) //i=0时,不构成插入排序的定义 因为0元素之前没有有序子序列 
    54    {
    55            Insertion(a,i); 
    56            if(match(sorttemp,a,num))//if(match) 再处理一轮+输出+return 
    57            {
    58                    if(i<num-1) i++;  //i++易漏掉  one more iteration
    59                    Insertion(a,i);
    60                    printf("Insertion Sort
    ");
    61                    print(a,num);
    62                    return 0; 
    63            }
    64    }
    65     
    66    
    67    for(int i=0;i<num;i++) a[i]=data[i];
    68    int len=num,temp=0;   
    69   
    70    for(int i=num/2-1;i>=0;i--) //构建大根堆
    71            headshift(a,i,len);           
    72   
    73    for(int i=num-1;i>=0;i--) //排序             
    74    {
    75            temp=a[0],a[0]=a[i],a[i]=temp;//顶尾交换  表示当前最大值已处理    
    76            len--;//堆长度-1
    77            headshift(a,0,len);//headshift新顶
    78            if(match(sorttemp,a,num))//if(match) 再处理一轮+输出+return 
    79            {
    80                    i--; //i-- 易漏掉  one more iteration
    81                    temp=a[0],a[0]=a[i],a[i]=temp;
    82                    len--;
    83                    headshift(a,0,len);
    84                    printf("Heap Sort
    ");
    85                    print(a,num);
    86                    return 0;
    87            }
    88    }
    89     return 0;
    90 }
    91  


  • 相关阅读:
    获取浏览器当前宽高
    获取当前页面一个 CSS 像素与一个物理像素之间的比率
    获取对象的所有属性,不管是否可遍历,不管是自身的还是原型链上的
    获取当前页面内所有框架窗口
    获取当前页面视口(viewport)宽高
    获取当前嵌入窗口所在的那个元素节点
    获取当前页面内框架窗口的数量
    获取窗口顶层对象
    获取当前窗口访问过的页面的数量
    获取`script`标签中的代码内容
  • 原文地址:https://www.cnblogs.com/asinlzm/p/4460692.html
Copyright © 2011-2022 走看看