浙大pat | 浙大pat 牛客网甲级 1098. Insertion or Heap Sort (25)判断是哪种排序方式
2018-03-23 12:49
721 查看
题目描述According to Wikipedia:
Insertion sort iterates, consuming one inputelement each repetition, and growing a sorted output list. Each iteration,insertion sort removes one element from the input data, finds the location itbelongs within the sorted list, and inserts it there. It repeats until no inputelements remain.
Heap sort divides its input into a sorted andan unsorted region, and it iteratively shrinks the unsorted region byextracting the largest element and moving that to the sorted region. itinvolves the use of a heap data structure rather than a linear-time search tofind the maximum.
Now given the initial sequence of integers, together with a sequence which is aresult of several iterations of some sorting method, can you tell which sortingmethod we are using?
输入描述:
Each input file contains one test case. For each case, the first line gives apositive integer N (<=100). Then inthe next line, N integers are given as the initial sequence. The last line contains the partially sortedsequence of the N numbers. It is assumedthat the target sequence is always ascending. All the numbers in a line are separated by a space.
输出描述:
For each test case, print in the first line either"Insertion Sort" or "Heap Sort" to indicate the method usedto obtain the partial result. Then runthis method for one more iteration and output in the second line the resulingsequence. It is guaranteed that theanswer is unique for each test case. Allthe numbers in a line must be separated by a space, and there must be no extraspace at the end of the line.
输入例子:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
输出例子:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
这题真的是,很多条件都没说明白
首先,运算的过程和题目开头给出的堆排序和插入排序的内容是没关系的,题目中要求你通过一个初始的数组和一个中间数组得到它是使用了堆排序还是插入排序,这里再理解上有一个难点就是堆排序和插入排序的中间过程到底是什么意思,首先插入排序的中间过程是在同一个数组上进行的,堆排序的中间过程也是在同一个数组上进行的,所以我们需要自己体会这一点,其实两个过程都是在同一个数组上进行的。
还有就是以后在做这种在某种条件下输出某个字符串的题目的时候,需要看清楚题目中给的字符串到底是什么!
以后在做题的时候,题目中让你使用某个算法但是有没有说明这个算法的过程的时候,按照这个算法在教材上传统的过程来计算就能够得到结果!
Insertion sort iterates, consuming one inputelement each repetition, and growing a sorted output list. Each iteration,insertion sort removes one element from the input data, finds the location itbelongs within the sorted list, and inserts it there. It repeats until no inputelements remain.
Heap sort divides its input into a sorted andan unsorted region, and it iteratively shrinks the unsorted region byextracting the largest element and moving that to the sorted region. itinvolves the use of a heap data structure rather than a linear-time search tofind the maximum.
Now given the initial sequence of integers, together with a sequence which is aresult of several iterations of some sorting method, can you tell which sortingmethod we are using?
输入描述:
Each input file contains one test case. For each case, the first line gives apositive integer N (<=100). Then inthe next line, N integers are given as the initial sequence. The last line contains the partially sortedsequence of the N numbers. It is assumedthat the target sequence is always ascending. All the numbers in a line are separated by a space.
输出描述:
For each test case, print in the first line either"Insertion Sort" or "Heap Sort" to indicate the method usedto obtain the partial result. Then runthis method for one more iteration and output in the second line the resulingsequence. It is guaranteed that theanswer is unique for each test case. Allthe numbers in a line must be separated by a space, and there must be no extraspace at the end of the line.
输入例子:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
输出例子:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
这题真的是,很多条件都没说明白
首先,运算的过程和题目开头给出的堆排序和插入排序的内容是没关系的,题目中要求你通过一个初始的数组和一个中间数组得到它是使用了堆排序还是插入排序,这里再理解上有一个难点就是堆排序和插入排序的中间过程到底是什么意思,首先插入排序的中间过程是在同一个数组上进行的,堆排序的中间过程也是在同一个数组上进行的,所以我们需要自己体会这一点,其实两个过程都是在同一个数组上进行的。
还有就是以后在做这种在某种条件下输出某个字符串的题目的时候,需要看清楚题目中给的字符串到底是什么!
以后在做题的时候,题目中让你使用某个算法但是有没有说明这个算法的过程的时候,按照这个算法在教材上传统的过程来计算就能够得到结果!
#include <iostream> #include <string> using namespace std; int bNum[103]; int num[103]; int main() { int N; int tmp; string heapOrInsert; cin >> N; for (int i = 1; i <= N; i++) { cin >> bNum[i]; } for (int i = 1; i <= N; i++) { cin >> num[i]; } if(num[2]>num[1]) { heapOrInsert = "Insertion Sort"; for (int i = 2; i <= N; i++) { if (num[i] < num[i - 1]) { for (int j = i; j >= 1; j--) { if (num[j] < num[j-1]) swap(num[j], num[j-1]); else break; } break; } } } else { int tmp,l; heapOrInsert = "Heap Sort"; for (int i = N; i >= 1; i--) { if (num[i] < num[1]) { tmp = i; swap(num[1], num[i]); break; } } tmp--; for (int i = 1; i <= tmp; ) { l = i * 2; if (l > tmp) break; if (l+1<=tmp&&num[l + 1] > num[l]) l++; if (num[i] < num[l]) swap(num[i], num[l]); i = l; } } cout << heapOrInsert << endl; for (int i = 1; i <= N; i++) { cout << num[i]; if (i != N) cout << " "; } return 0; }
相关文章推荐
- PAT - 甲级 - 1098. Insertion or Heap Sort (25)(排序)
- PAT 甲级 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)-PAT甲级真题(堆排序)
- PAT甲级 1098. Insertion or Heap Sort (25)
- pat 甲级 1098. Insertion or Heap Sort (25)
- PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)
- 浙大pat | 浙大pat 牛客网甲级1061 Insert or Merge (25)判断是插入排序还是归并排序
- PAT 1098-Insertion or Heap Sort (25)
- PAT (Advanced Level) Practise 1098 Insertion or Heap Sort (25)
- 【PAT】1098. Insertion or Heap Sort (25)
- [PAT]1098. Insertion or Heap Sort (25)
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
- 浙大pat | 牛客网甲级 1025 Sort with Swap(0) (25)排序
- PAT 1098 Insertion or Heap Sort (25)(待修改)
- 【PAT】【Advanced Level】1098. Insertion or Heap Sort (25)
- PAT(Advance) 1098. Insertion or Heap Sort (25)
- PAT 1098. Insertion or Heap Sort (25)
- PAT程序设计考题——甲级1098(Insertion or Heap Sort ) C++实现
- PAT-A-1098. Insertion or Heap Sort (25)
- PAT 1098. Insertion or Heap Sort (25) 又掉出题人的语言陷阱里了!!