第一题
题目描述:
定义包含学号、姓名和成绩的学生信息结构类型,完成以下功能:
(1)键盘输入n个学生信息数据(n从键盘输入);
(2)使用“冒泡法”对学生成绩由高到低排序;
(3)输出排序后学生信息。
输入输出样例
输入样例:
3
100001 wang-li 91100002 chen-da-wei 95
100003 guo-tao 93
输出样例:100002 chen-da-wei 95
100003 guo-tao 93
100001 wang-li 91
样例输入:
2
111111 wang 78
222222 liu 100
样例输出:
222222 liu 100
111111 wang 78
参考代码
#include<stdio.h>
struct student
{
int num;
char name[20];
float score;
}stu[100];
void sort(struct student stu[], int n)
{
int i, j;
struct student t;//再声明一个
for (i = 0; i < n - 1; i++)//标准冒泡排序
{
for (j = i + 1; j < n; j++)
{
if (stu[i].score<stu[j].score)
{
t = stu[i];
stu[i] = stu[j];
stu[j] = t;
}
else if (stu[i].score == stu[j].score)//当成绩一样的时候
{
if (stu[i].num < stu[j].num)
{
t = stu[i];
stu[i] = stu[j];
stu[j] = t;
}
}
}
}
}
int main()
{
int n;
int i;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &stu[i].num);
scanf("%s", &stu[i].name);
scanf("%f", &stu[i].score);
}
sort(stu, n);
for (i = 0; i < n; i++)
printf("%d %s %1.0f\n", stu[i].num, stu[i].name, stu[i].score);//为什么要加1.0?
return 0;
}
第二题
题目描述:
从键盘输入奇数个整型数(大于3),对该整数序列按如下规则排序:最大值排在中间,最小值排在最左,中值排在最右,其它值清为0。
定义排序函数原型为:void booble(int *array,int n);
运行样例如下所示:
输入
5
1 2 3 4 5
输出
1 0 5 0 3
样例输入:
7
10 1 1 100 0 0 5
样例输出:
0 0 0 100 0 0 1
参考代码
#include<stdio.h>
#include<stdlib.h>
void booble(int*array, int n)
{
int temp;
for (int i = 0; i < n - 1; i++) //冒泡排序
for (int j = 0; j < n - 1; j++)
{
if (*(array + j) > *(array + j