函数题:万能的排序函数
有n个整数,需要对他们进行各种排序:升序排序、降序排序、按个位数升序排序、按十位数降序排序,为了提高代码的重用性,可以用指向函数的指针作排序函数的参数。<br>
程序输入n+2个整数:<br>
第一行是整数n,表示后面有n个整数需要排序<br>
第二行是待排序的n个整数<br>
第三行是排序需求,分别是1-4,依次表示升序排序、降序排序、按个位数升序排序、按十位数降序排序<br>
程序的输出是排好序的n个整数,两个整数之间用空格隔开
### 函数接口定义:
需要实现以下函数:
c++
void sort(int *p, int (*compare)(int a,int b), int N);
int Descending(int a, int b);
int bit1_Des(int a, int b);
其中 p 是数据的首地址, N 是需要排序的数据个数, compare 是指向函数的指针。sort函数用选择法进行排序
### 裁判测试程序样例:
c++
#include <stdio.h>
void sort(int *p, int (*compare)(int a,int b), int N);
int Ascending(int a, int b);
int Descending(int a, int b);
int bit0_Asc(int a, int b);
int bit1_Des(int a, int b);
int main()
{ int n=10,k;
int *s,*p;
int (*fun)(int,int);
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
for(s=p; s<p+n; s++)
scanf("%d",s);
scanf("%d",&k);
switch(k)
{
case 1:fun=Ascending; break;
case 2:fun=Descending; break;
case 3:fun=bit0_Asc; break;
case 4:fun=bit1_Des; break;
}
sort(p,fun,n);
for(s=p; s<p+n-1; s++)
printf("%d ",*s);
printf("%d\n",*s);
return 0;
}
int Ascending(int a, int b)
{ return a>b;
}
int bit0_Asc(int a, int b)
{
return a%10>b%10;
}
/* 请在这里填写答案 */
### 输入样例:
in
11
886 461 856 581 514 272 197 182 564 121 894
3
### 输出样例:
out
461 581 121 272 182 514 564 894 856 886 197
答案:若无答案欢迎评论
程序输入n+2个整数:<br>
第一行是整数n,表示后面有n个整数需要排序<br>
第二行是待排序的n个整数<br>
第三行是排序需求,分别是1-4,依次表示升序排序、降序排序、按个位数升序排序、按十位数降序排序<br>
程序的输出是排好序的n个整数,两个整数之间用空格隔开
### 函数接口定义:
需要实现以下函数:
c++
void sort(int *p, int (*compare)(int a,int b), int N);
int Descending(int a, int b);
int bit1_Des(int a, int b);
其中 p 是数据的首地址, N 是需要排序的数据个数, compare 是指向函数的指针。sort函数用选择法进行排序
### 裁判测试程序样例:
c++
#include <stdio.h>
void sort(int *p, int (*compare)(int a,int b), int N);
int Ascending(int a, int b);
int Descending(int a, int b);
int bit0_Asc(int a, int b);
int bit1_Des(int a, int b);
int main()
{ int n=10,k;
int *s,*p;
int (*fun)(int,int);
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
for(s=p; s<p+n; s++)
scanf("%d",s);
scanf("%d",&k);
switch(k)
{
case 1:fun=Ascending; break;
case 2:fun=Descending; break;
case 3:fun=bit0_Asc; break;
case 4:fun=bit1_Des; break;
}
sort(p,fun,n);
for(s=p; s<p+n-1; s++)
printf("%d ",*s);
printf("%d\n",*s);
return 0;
}
int Ascending(int a, int b)
{ return a>b;
}
int bit0_Asc(int a, int b)
{
return a%10>b%10;
}
/* 请在这里填写答案 */
### 输入样例:
in
11
886 461 856 581 514 272 197 182 564 121 894
3
### 输出样例:
out
461 581 121 272 182 514 564 894 856 886 197
答案:若无答案欢迎评论