题目描述:
现有一学生管理系统,包含N个学生姓名与学生成绩,你需要回答Q个操作,这些询问包括:
求平均值
求最大值
求最小值
改变某个同学的成绩。
使用冒泡排序按学生姓名字典序升序排序
使用选择排序按成绩降序排序,若成绩相同,则按学生姓名升序排序
查找某同学在名单中的位置,下标从1开始,下同
插入在某位置插入某同学及其成绩
删除某同学及其成绩
输入要求:
测试数据有多组
输入的第一行输入一个整数T,代表测试组数
每组测试数据第一行有两个整数N, M (1 <= N <= 100, 1 <= M <= 100)
接下来N行输入N个学生的姓名和成绩,保证每个同学的姓名各不相同,成绩都是非负整数
接下来M行输入M个操作,这些操作的格式包括:
QUERY_AVERAGE: 求平均值
QUERY_MAX: 求最大值
QUERY_MIN: 求最小值
CHANGE index score: 将存在于名单中下标为index(下标从1开始)的同学的成绩修改为score,输入保证合法
BUBBLE_SORT_BY_NAME_ASC: 使用冒泡排序按学生姓名字典序升序排序,输出每一趟排序后的结果中的姓名,最后输出学生及其成绩名单
SELECTION_SORT_BY_GRADE_DESC: 使用选择排序按成绩降序排序,若成绩相同,则按学生姓名升序排序,输出每一趟排序后的结果中的成绩,最后输出学生及其成绩名单
FIND name: 查找名字位name的同学在名单中的位置,若不存在则输出 "NOT FOUND",若存在不输出
INSERT index name score: 在下标为index的位置插入名字为name,分数为score的同学,若名字已存在名单中则输出"ERROR" 。若插入位置不在1~学生数+1, 即位置不合法,输出"ERROR"。
DELETE name: 在名单中删除名字为name的同学,若不存在该同学则输出"ERROR"
输出要求:
具体输出见样例
每组样例后面输出一行空行
#include<iostream>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstring>
using namespace std;
void QUERY_AVERAGE(int score[], int n)
{
float average, sum = 0;
for (int i = 1; i < n + 1; i++)
{
sum += score[i];
}
average = sum / n;
printf("%0.1lf\n", average);
}
void QUERY_MAX(int score[], int n)
{
int max0 = 0xc0c0c0c0;
for (int i = 1; i < n + 1; i++)
{
max0 = max(max0, score[i]);
}
cout << max0 << endl;
}
void QUERY_MIN(int score[], int n)
{
int min0 = 0x3f3f3f3f;
for (int i = 1; i < n + 1; i++)
{
min0 = min(min0, score[i]);
}
cout << min0 << endl;
}
void BUBBLE_SORT_BY_NAME_ASC(char name[][111], int score[], int n)
{
for (int i = 1; i < n; i++)
{
for (int j = 1; j < n - i + 1; j++)
{
if (strcmp(name[j], name[j + 1]) > 0)
{
swap(name[j], name[j + 1]);
swap(score[j], score[j + 1]);
}
}
for (int k = 1; k < n; k++)
{
cout << name[k] << " ";
}
cout << name[n] << endl;
}
for (int i = 1; i < n + 1; i++)
{
cout << name[i] << " " << score[i] << endl;
}
}
void SELECTION_SORT_BY_GRADE_DESC(char name[][111], int score[], int n)
{
int script;
for (int i = 1; i < n; i++)//选择排序
{
script = i;
for (int j = i + 1; j < n + 1; j++)
{
if (score[script] < score[j])//成绩降序
{
script = j;
}
else if ((score[script] == score[j]) && (strcmp(name[script], name[j]) > 0))//成绩相同,姓名升序
{
script = j;
}
}
if (script != i)//如果位置改变,就交换名字和成绩
{
swap(score[i], score[script]);
swap(name[i], name[script]);
}
for (int k = 1; k < n; k++)
{
cout << score[k] << " ";
}
cout << score[n] << endl;
}
for (int i = 1; i < n + 1; i++)
{
cout << name[i] << " " << score[i] << endl;
}
}
int FIND(char name[][111], char namef[], int n)
{
for (int i = 1; i < n + 1; i++)
{
if (strcmp(name[i], namef) == 0)//存在就返回,不存在就返回0
{
return i;
}
}
return 0;
}
int INSERT(char name[][111], char namef[], int score[], int n, int index, int score8)
{
if ((FIND(name, namef, n)) || (index < 1) || (index > n + 1))//如果存在或者下标错误,就输出error
{
cout << "ERROR" << endl;
return 0;
}
else
{
if (index == n + 1)//在最后就直接插入
{
score[n] = score8;
strcpy(name[index], namef);
}
else
{
for (int i = n; i >= index; i--)//在中间就挨个插入
{
score[i + 1] = score[i];
strcpy(name[i + 1], name[i]);
}
score[index] = score8;
strcpy(name[index], namef);
}
}
return 1;
}
int DELETE(char name[][111], char namef[], int score[], int n)
{
if (FIND(name, namef, n))
{
int tag = FIND(name, namef, n);//确定位置
for (int i = tag; i < n + 1; i++)//去除空出来的位置
{
score[i] = score[i + 1];
strcpy(name[i], name[i + 1]);
}
return 1;
}
else
{
return 0;
}
}
int main() {
int t;
cin >> t;
for (int t0 = 1; t0 <= t; t0++)
{
int n, m;
cin >> n >> m;
char name[111][111];
int score[111];
for (int i = 1; i < n + 1; i++)
{
cin >> name[i] >> score[i];
}
while (m--)
{
char ch[30];
char ch1[] = "QUERY_AVERAGE";//求平均值
char ch2[] = "QUERY_MAX";//求最大值
char ch3[] = "QUERY_MIN";//求最小值
char ch4[] = "CHANGE";//改变某个同学成绩
char ch5[] = "BUBBLE_SORT_BY_NAME_ASC";//使用冒泡排序按学生姓名字典序升序排序
char ch6[] = "SELECTION_SORT_BY_GRADE_DESC";//使用选择排序按成绩降序排序,若成绩相同,则按学生姓名升序排序
char ch7[] = "FIND";//查找某同学在名单中的位置,下标从1开始,下同
char ch8[] = "INSERT";//插入在某位置插入某同学及其成绩
char ch9[] = "DELETE";//删除某同学及其成绩
cin >> ch;
if (strcmp(ch, ch1) == 0)
{
QUERY_AVERAGE(score, n);
}
if (strcmp(ch, ch2) == 0)
{
QUERY_MAX(score, n);
}
if (strcmp(ch, ch3) == 0)
{
QUERY_MIN(score, n);
}
if (strcmp(ch, ch4) == 0)
{
int index, now;
cin >> index >> now;
score[index] = now;
}
if (strcmp(ch, ch5) == 0)
{
BUBBLE_SORT_BY_NAME_ASC(name, score, n);
}
if (strcmp(ch, ch6) == 0)
{
SELECTION_SORT_BY_GRADE_DESC(name, score, n);
}
if (strcmp(ch, ch7) == 0)
{
char namef[101];
cin >> namef;
if (FIND(name, namef, n) == 0)
{
cout << "NOT FOUND" << endl;
}
}
if (strcmp(ch, ch8) == 0)//直接插入
{
int index, score8;
char namef[101];
cin >> index;
cin >> namef;
cin >> score8;
if (INSERT(name, namef, score, n, index, score8))
{
n++;
}
}
if (strcmp(ch, ch9) == 0)//直接删除
{
char namef[101];
cin >> namef;
if (DELETE(name, namef, score, n))
{
n--;
}
}
}
if (t0 != t)
{
cout << endl;
}
}
return 0;
}