题意:

输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组。判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一)。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int c[][];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
for(int i=;i<=n;++i)
cin>>b[i];
int pos=;
for(int i=;i<n;++i)
if(b[i]>b[i+]){
pos=i;
break;
}
int pos2=;
for(int i=pos+;i<=n;++i)
if(a[i]!=b[i]){
pos2=i;
break;
}
if(!pos2){
cout<<"Insertion Sort\n";
int flag=,mn=b[pos+];
int ppos=;
for(int i=;i<=n;++i)
if(b[i]>mn){
ppos=i;
break;
}
for(int i=pos+;i>ppos;--i)
b[i]=b[i-];
b[ppos]=mn;
for(int i=;i<=n;++i){
cout<<b[i];
if(i<n)
cout<<" ";
}
}
else{
cout<<"Heap Sort\n";
int ppos=;
sort(a+,a++n);
for(int i=n;i;--i)
if(b[i]!=a[i]){
ppos=i;
break;
}
int mx=,flag=;
for(int i=;i<=ppos;++i)
if(b[i]>mx){
mx=b[i];
flag=i;
}
swap(b[flag],b[ppos]);
int s=;
while(s<ppos){
if(b[s*]>b[s]&&b[s*+]>b[s]&&ppos>s*+){
if(b[s*]>b[s*+]){
swap(b[s],b[s*]);
s=s*;
}
else{
swap(b[s],b[s*+]);
s=s*+;
}
}
else if(b[s*+]>b[s]&&ppos>s*+){
swap(b[s],b[s*+]);
s=s*+;
}
else if(b[s*]>b[s]&&ppos>s*){
swap(b[s],b[s*]);
s=s*;
}
if(s*>=ppos)
break;
}
for(int i=;i<=n;++i){
cout<<b[i];
if(i<n)
cout<<" ";
}
}
return ;
}

【PAT甲级】1098 Insertion or Heap Sort (25 分)的更多相关文章

  1. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  2. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  3. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  4. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  5. 1098 Insertion or Heap Sort (25分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  6. PTA 09-排序3 Insertion or Heap Sort (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort   (25分) Accor ...

  7. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  8. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  9. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

随机推荐

  1. Log4j的isdebugEnabled的作用

    转自:https://www.iteye.com/blog/zhukewen-java-1174017 在项目中我们经常可以看到这样的代码: if (logger.isDebugEnabled()) ...

  2. windows10 +ubuntu双系统

    1,安装之前的准备: 制作启动盘 确定给ubuntu多少分区并且清理为free状态 确定电脑的开机引导方式,传统方式引号和uefi引导并不一样,因此我们需要根据引导方式选择新系统制作什么样的启动盘 在 ...

  3. ECharts实现拖拽

    1.需求——如何在echarts中实现托拖拽的效果 2.解决方案 1.用Echarts的type为graph类型画图,优点:拖拽不用重新定点划线,有自带的连线关系,缺点:连线的样式及特效不能很好呈现 ...

  4. 将Tomcat集成到idea中

    检查有没有成功

  5. 将项目部署到linux环境下的Jetty

    1.将项目放到webapps文件夹下 2.进入到jetty/bin目录,有文件jetty.sh 3.运行  命令:./jetty.sh start 4.停止  命令:./jetty.sh stop

  6. 如何查看mac多少位的操作系统?

    1.点击工具栏左上角点击 (苹果Logo)标志,关于本机  -->  更多信息 --> 系统报告  -->(左侧栏中)软件 (有的电脑是没有的例如第一张图) 2. 输入命令 una ...

  7. 找到所有的txt文件并删除

    1.find /oldboy/ -type f -name "*.txt" -delete 2.find /oldboy/ -type f -name "*.txt&qu ...

  8. nvalidSchema: Missing dependencies for SOCKS support

    首先需要安装pip3 1. 安装 setuptools wget --no-check-certificate https://pypi.python.org/packages/source/s/se ...

  9. [刷题] Leetcode算法 (2020-2-27)

    1.最后一个单词的长度(很简单) 题目: 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度. 如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词. 如果不存在 ...

  10. MyEcplise中编码格式的修改问题

    1.如果是在Run Configurations中修改编码格式的话,只能是修改当前java文件的编码格式,把改文件中的代码复制到 另一新建 的java文件中会出现异常,所以就会出现相同的代码在两个不同 ...