程序设计基础54 two_pointers PAT A1089

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<cstdio>
#include<algorithm>
using namespace std;
const int max_n = 110;
int arr[max_n], copy_arr[max_n], target[max_n], ans[max_n];
void copy_same(int A[], int B[], int len) {
	for (int i = 0; i < len; i++) {
		B[i] = A[i];
	}
}
bool is_same_arr(int A[], int B[], int len) {
	for (int i = 0; i < len; i++) {
		if (A[i] != B[i]) {
			return false;
		}
	}
	return true;
}
void print_arr(int A[], int len) {
	for (int i = 0; i < len; i++) {
		printf("%d", A[i]);
		if (i != len - 1) {
			printf(" ");
		}
	}
}
bool insert_sort(int A[], int len) {
	bool flag = false;
	int num = 0;
	for (int i = 1; i < len; i++) {
		if (flag == false) {
			if (i != 1 && is_same_arr(A, target, len)) {
				flag = true;
			}
			int temp = A[i];
			int j = i;
			while (j > 0 && A[j - 1] > temp) {
				A[j] = A[j - 1];
				j--;
			}
			A[j] = temp;
			if (flag == true) {
				return flag;
			}
		}
	}
	return flag;
}
void merge(int A[], int R1, int L1, int R2, int L2) {
	int i = R1, j = R2;
	int index = 0;
	int temp[max_n];
	while (i <= L1&&j <= L2) {
		if (A[i] <= A[j])temp[index++] = A[i++];
		else temp[index++] = A[j++];
	}
	while (i <= L1)temp[index++] = A[i++];
	while (j <= L2)temp[index++] = A[j++];
	for (int i = 0; i < index; i++) {
		A[R1 + i] = temp[i];
	}
}
bool merge_sort(int A[],int len) {
	bool flag = false;
	for (int step = 2; step / 2 < len; step *= 2) {
		if (flag == false) {
			if (step != 2 && is_same_arr(A, target, len)) {
				flag = true;
			}
			for (int i = 0; i < len; i += step) {
				int mid = i + step / 2 - 1;
				if (mid + 1 < len) {
					merge(A, i, mid, mid + 1, min(i + step - 1, len - 1));
				}
			}
			if (flag == true) {
				return flag;
			}
		}
	}
	return flag;
}
int main() {
	int N = 0;
	bool flag = false;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	for (int i = 0; i < N; i++) {
		scanf("%d", &target[i]);
	}
	copy_same(arr, copy_arr, N);
	flag = insert_sort(copy_arr, N);
	if (flag == true) {
		printf("Insertion Sort\n");
		print_arr(copy_arr, N);
	}
	else {
		copy_same(arr, copy_arr, N);
		printf("Merge Sort\n");
		flag = merge_sort(copy_arr, N);
		print_arr(copy_arr, N);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值