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, ...
随机推荐
- 用vbs和ADSI管理Windows账户
ADSI (Active Directory Services Interface)是Microsoft新推出的一项技术,它统一了许多底层服务的编程接口,程序员可以使用一致的对象技术来访问这些底层服务 ...
- 一文学会 TypeScript 的 82% 常用知识点(上)
前端专栏 2019-11-22 19:17:55 对于前端从业者来说,TypeScript(以下简称 TS)已经不算是新技术. Vue3 的源码基于 TS 编写, Angular 项目默认支持 TS ...
- 05-Django模型(2)
1.特殊查询 F和Q查询: 之前的查询都是模型对象的属性与常量值比较,两个属性怎么比较呢?使用F查询. F查询语法: from django.db.models import F F('属性名称') ...
- js使用“toFixed( )”保留小数点后两位
例如: var a = 1.335; alert(a.toFixed(2)) // IE 1.34 //chorme 1.33 若a为字符串,则需要先转换为Number类型 如: n = Number ...
- 使用c#创建Excel 2013外接程序
心好累,印象笔记国内版和国际版账号还不能通用,在国内版写了一个没法创建共享链接(只有共享给XXemail),于是又写了一遍到国际版上(因为图片无法复制,又copy了一遍图片),现在copy到博客园,图 ...
- docker下安装Redis
Docker介绍 1.节约时间.快速部署和启动 2.节约成本 3.标准化应用发布 4.方便做持续继承 5作为集群中的轻量主机或节点 6.方便构建基于SOA或者微服务架构的系统 Docker中文文档 h ...
- js监听屏幕方向如何第一次默认不监听
this.supportOrientation = typeof window.orientation === 'number'; // 检查屏幕方向 checkScreenOrientation() ...
- sockjs+stomp的websocket插件
/** * 依赖文件sockjs.js.stomp.js * */ ;!(function (window) { 'use strict' let WS = function () { //保存所有的 ...
- ICMP重定向 Redirect netwox libpcap netwag
搭建环境 两台虚拟机. 攻击者:192.168.1.8 被攻击者:192.168.1.9 网络配置 主机均采用DHCP 如果没有路由器,可以使用NAT模式来做 攻击者配置 打开转发数据包功能 # su ...
- mssql sqlserver sql脚本自动遍历重复生成指定表记录
摘要: 今天接到老板的需求,需根据一张表中列值,自动重复表中的数据行,然后显示给用户 实验环境:sqlserver 2008 R2 转自:http://www.maomao365.com/?p=841 ...