zoukankan      html  css  js  c++  java
  • 1089 Insert or Merge (25 分)

    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.

    Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

    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 "Merge 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 0 6
    1 3 2 8 5 7 4 9 0 6
    

    Sample Output 2:

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


      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 
      4 const int maxn=110;
      5 
      6 int a[maxn],b[maxn],c[maxn];
      7 
      8 
      9 int n;
     10 
     11 bool judge(int*a,int *b) 
     12 {
     13     for(int i=0;i<n;i++)
     14     {
     15         if(a[i]!=b[i])
     16             return false;
     17     }
     18     
     19     return true;
     20 }
     21 
     22 
     23 void print(int* a){
     24     for(int i=0;i<n;i++){
     25         
     26         if(i>0)
     27             cout<<" ";
     28         
     29         cout<<a[i];    
     30     }
     31     cout<<endl;
     32 }
     33 
     34 
     35 bool InsertSort(){
     36     
     37     bool flag=false;
     38     
     39     
     40     for(int i=1;i<n;i++){
     41         
     42         int j=i;
     43         
     44         int temp=b[j];
     45     
     46         
     47         if(i!=1&&judge(b,c)){
     48             flag=true;
     49         }
     50         
     51         while(j>0&&temp<b[j-1])//这里错把temp写成b[j]了,要细心
     52         {
     53             b[j]=b[j-1];
     54             j--;    
     55         }
     56         
     57         b[j]=temp;
     58         
     59         
     60         if(flag){
     61             
     62             return true;
     63         }
     64                     
     65     }
     66     
     67     
     68     return false;
     69     
     70 }
     71 
     72 
     73 
     74 void MergeSort(){
     75     
     76     bool flag=false;
     77     
     78     for(int step=2;step/2<=n;step*=2){
     79     
     80         if(step!=2&&judge(b,c)){
     81             flag=true;
     82         }
     83         
     84         for(int i=0;i<n;i+=step){
     85             
     86             sort(b+i,b+min(i+step,n));
     87         }
     88         
     89         if(flag){
     90             cout<<"Merge Sort
    ";
     91             print(b);
     92             return;
     93         }
     94         
     95         
     96     }    
     97         
     98 }
     99 
    100 
    101 
    102 
    103 int main(){
    104     cin>>n;
    105     
    106     for(int i=0;i<n;i++){
    107         cin>>a[i];
    108         b[i]=a[i];
    109     }
    110     
    111     for(int i=0;i<n;i++){
    112         cin>>c[i];
    113     }
    114     
    115     if(InsertSort()){
    116         
    117         cout<<"Insertion Sort
    ";
    118         
    119         print(b);
    120     }else{
    121         
    122         for(int i=0;i<n;i++)
    123             b[i]=a[i];
    124             
    125         MergeSort(); 
    126     }
    127     
    128     return 0;    
    129 }


  • 相关阅读:
    团购的玩法 要粘性也要乐趣
    风讯CMS提交时出现System.Web.HttpRequestValidationException (0x80004005):错误
    关于传统零售企业网上经营模式的探讨
    一个很漂亮的转入别的页面时等待页
    采集百度图片
    带有立体感的凹陷字体,非常夺人眼球
    sql2005 远程连接问题解决方法
    草根创业:接纳与淘汰终归“剩”者为王
    写一个属于自己的模板引擎(1)
    写一个属于自己的模板引擎(2)
  • 原文地址:https://www.cnblogs.com/moranzju/p/11157856.html
Copyright © 2011-2022 走看看