A1089 Insert or Merge (25 分)
一、技术总结
- 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现
- 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所以知道当发现有位置当前数比后面大时,进而再判断后面的每一位是否啊a、b数组都相等,如果是那么就是插入排序,再在后面进行一轮排序即可。如果不是就是归并排序。最主要的问题是判断归并排序到哪一轮了,使用while循环模拟归并排序,对数组a进行归并排序,直到与数组b相等,再进行一轮排序就是输出结果了。
二、参考代码
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[100], b[100], i, j, n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i];
}
for(i = 0; i < n-1 && b[i] <= b[i+1]; i++);
for(j = i+1; j < n && a[j] == b[j]; j++);
if(j == n){
printf("Insertion Sort\n");
sort(a, a+i+2);
}else{
printf("Merge Sort\n");
int flag = 1, k = 1;
while(flag){
flag = 0;
for(i = 0; i < n; i++){
if(a[i] != b[i]){
flag = 1;
}
}
k = k*2;
for(i = 0; i < n/k; i++){
sort(a+i*k, a+(i+1)*k);
}
sort(a+n/k*k, a+n);
}
}
for(j = 0; j < n; j++){
if(j != 0){
printf(" ");
}
cout << a[j];
}
return 0;
}
A1089 Insert or Merge (25 分)的更多相关文章
- PTA 09-排序2 Insert or Merge (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge (25分) According to ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- 09-排序2 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- pat1089. Insert or Merge (25)
1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...
- PAT 1089. Insert or Merge (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089. Insert or Merge (25)
题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...
随机推荐
- Tomcat 设置JVM内存大小
我的服务器的配置: # OS specific support. $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...
- __module__和__class__
目录 一.__module__ 二.__class__ # lib/aa.py class C: def __init__(self): self.name = 'SB' # index.py fro ...
- ssh 免密码登录服务器
本机生成 ssh key ssh-keygen -t rsa -C "your_email@example.com" 上传公钥文件(假设用户为 user,服务器 ip 为 1.2. ...
- IT兄弟连 HTML5教程 HTML5技术的应用现状及HTML5平台的兴起
HTML5的优良特性很快被各种类型的网站利用,比如文件拖拽到网页上传功能,多数即使用HTML5提供的新属性就可以完成,来实现素材的免插件拖放.因此,HTML5技术实际上在国内已经获得了较广泛的应用与支 ...
- D - Ugly Problem HDU - 5920
D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must ...
- MVC教程:授权过滤器
一.过滤器 过滤器(Filter)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,并不是每个请求都会响应内容,只有那些有特定权限的用户才能响应特定的内容.过滤器理论上 ...
- Python 使用 PyMysql、DBUtils 创建连接池,提升性能
转自:https://blog.csdn.net/weixin_41287692/article/details/83413775 Python 编程中可以使用 PyMysql 进行数据库的连接及诸如 ...
- 易优CMS:type的基础用法
[基础用法] 名称:type 功能:获取指定栏目信息 语法: {eyou:type typeid='栏目ID' empty='暂时没有数据'} <a href="{$field.typ ...
- vi 上下左右变ABCD乱码解决方法
CentOS echo "set nocompatible" >> ~/.vimrc source ~/.vimrc debian sudo apt-get remov ...
- Nginx反向代理实现负载均衡以及session共享
随着社会的发展和科技水平的不断提高,互联网在人们日常生活中扮演着越来越重要的角色,同时网络安全,网络可靠性等问题日益突出.传统的单体服务架构已不能满足现代用户需求.随之而来的就是各种分布式/集群式的服 ...