第6周中级练习
注意,本周练习均是关于递归的,不用递归没有意义(即使可以不用)。
注意,本周练习均是关于递归的,不用递归没有意义(即使可以不用)。
注意,本周练习均是关于递归的,不用递归没有意义(即使可以不用)。
依照学术诚信条款,我保证此作业是本人独立完成的。
温馨提示:
1.本次作业属于Online Judge题目,提交后由系统即时判分。
2.学生可以在作业截止时间之前不限次数提交答案,系统将取其中的最高分作为最终成绩。
1递归插入排序(10分)
题目内容:
编写函数,对整型数组进行递归插入排序。编写函数,输入、输出数组元素。编写主函数,调用函数输入、排序、输出元素。整数个数不超过100个。
输入:在一行或多行中输入若干用空隔开的整数,输入-9999表示结束。
输出:在一行中输出整数,用一个空格隔开,末尾无空格。
输入样例:
3 2 1 5 4 -9999
输出样例:
1 2 3 4 5
时间限制:500ms内存限制:32000kb
C++
#include<iostream>
using namespace std;
int const N = 120;
int a[N];
void Insert(int a[], int n){
int i = n - 1;
int key = a[n];
while ((i >= 0) && (key < a[i]))
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = key;
return;
}
void InsertionSort(int a[], int n){
if (n > 0)
{
InsertionSort(a, n - 1);
Insert(a, n);
}
else
return;
}
int main()
{
cin >> a[0];
int n = 0;
while (a[n] != -9999) {
cin >> a[++n];
}
InsertionSort(a, n-1);
cout