第1关:比较相邻两个元素求最大值
任务描述
本关任务:编写函数通过比较数组相邻两个元素求数组最大值
测试说明
平台会对你编写的代码进行测试:
测试输入:
10
39 1 4 32 91 57 6 33 2 88
输入说明:
第一行为n,表示n个整数。
第二行为n个整数。
预期输出:
1 4 32 39 57 6 33 2 88 91
输出说明:
最后一个元素应该是最大值
代码如下
#include <stdio.h>
#include<stdlib.h>
void input(int *&a,int &n);
void output(int *a,int n);
void comp(int *a, int n);
void swap(int &a, int &b);
int main ()
{
int i,n;
int *a = NULL;
input (a,n);
comp(a, n) ;
output(a,n);
free(a);
return 0;
}
/**********定义函数**********/
/********** Begin **********/
void input(int *&a,int &n)
{
int i;
scanf("%d",&n);
if((a=(int *) malloc ((n)*sizeof(int)))==NULL)
{
printf("不能成功分配内存单元\n");
exit(0);
}
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void output(int *a,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void comp(int *a, int n){
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])
swap(a[i],a[i+1]);
}
}
void swap(int &a, int &b)
{
int t;
t=a;a=b;b=t;
}
/********** End **********/
第2关:冒泡排序
任务描述
本关任务:编写函数实现冒泡排序。
测试说明
平台会对你编写的代码进行测试:
测试输入:
10
29 62 73 90 46 43 38 76 52 93
输入说明:
第一行为n,表示n个整数。
第二行为n个整数。
预期输出:
29 62 73 90 46 43 38 76 52 93
29 62 73 46 43 38 76 52 90 93
29 62 46 43 38 73 52 76 90 93
29 46 43 38 62 52 73 76 90 93
29 43 38 46 52 62 73 76 90 93
29 38 43 46 52 62 73 76 90 93
29 38 43 46 52 62 73 76 90 93
29 38 43 46 52 62 73 76 90 93
29 38 43 46 52 62 73 76 90 93
代码如下
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
void input(int *&a,int & n);
void output(int *a,int n);
void comp(int *a, int n) ;
void swap(int &a, int &b);
void Bubble_sort(int *a,int n);
int main ()
{
int i,n;
int *a = NULL;
input (a,n);
Bubble_sort(a, n) ;
free(a);
return 0;
}
void input(int *&a,int & n)
{
int i;
scanf("%d",&n);
if((a=(int *) malloc ((n)*sizeof(int)))==NULL)
{
printf("不能成功分配内存单元\n");
exit(0);
}
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void output(int *a,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void swap(int &a, int &b)
{
int t;
t=a;a=b;b=t;
}
/**********定义Bubble_sort()函数**********/
/********** Begin **********/
void Bubble_sort(int *a,int n){
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int tmp = a[j + 1]; // 保存较小项值
a[j + 1] = a[j];
a[j]=tmp;
}
}
output(a,n);
}
}
/********** End **********/
第3关:快速排序的递归算法
任务描述
本关任务:编写函数实现快速排序的递归算法。
测试说明
平台会对你编写的代码进行测试,对数组中的元素实现快速排序,并输出每一次划分后数组元素的值,具体请参见后续测试样例。
测试输入:
10
49 82 68 71 48 27 69 23 87 17
输入说明:
第一行为n,表示n个整数。
第二行为n个整数。
预期输出:
17 23 27 48 49 71 69 68 87 82
17 23 27 48 49 71 69 68 87 82
17 23 27 48 49 71 69 68 87 82
17 23 27 48 49 71 69 68 87 82
17 23 27 48 49 68 69 71 87 82
17 23 27 48 49 68 69 71 87 82
17 23 27 48 49 68 69 71 82 87
输出说明:
输出每一次划分后数组元素的值。
代码如下
#include <stdio.h>
#include<stdlib.h>
void input(int *&a,int & n);
void output(int *a,int n);
void swap(int &a, int &b);
int Partition(int a[],int low,int high);
void QuickSort(int a[],int s,int t);
int n;
int main ()
{
int i;
int *a = NULL;
input (a,n);
QuickSort(a, 0, n-1) ;
free(a);
return 0;
}
void input(int *&a,int & n)
{
int i;
scanf("%d",&n);
if((a=(int *) malloc ((n)*sizeof(int)))==NULL)
{
printf("不能成功分配内存单元\n");
exit(0);
}
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void output(int *a,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void swap(int &a, int &b)
{
int t;
t=a;a=b;b=t;
}
/**********定义Partition() 和 QuickSort()函数**********/
/********** Begin **********/
int Partition(int a[],int low,int high){
int t,key=a[low];
while(low<high){
while(low<high && a[high]>=key) --high;
swap(a[low],a[high]);
while(low<high && a[low]<=key) ++low;
swap(a[low],a[high]);
}
return low;
}
void QuickSort(int a[],int s,int t){
int pivo;
if(s<t){
pivo=Partition(a,s,t);
output(a,n);
QuickSort(a,s,pivo-1);
QuickSort(a,pivo+1,t);
}
}
/********** End **********/