本文主要是介绍例题4-6 师兄帮帮忙(A Typical Homework (a.k.a Shi Xiong Bang Bang Mang),Rujia Liu's Present 5, UVa 12412),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题链接:https://vjudge.net/problem/UVA-12412
分类:函数
备注:复杂模拟
分析:特别繁琐,但理解清楚题意后实现起来不难,就是各种细节很麻烦,比如除数为0,它要求平均数输出-nan ???并且精度问题很重要,不管是单个人的平均成绩还是单科的平均成绩都要修正精度,看到要输出double的一定要记得精度问题!
代码如下:
#include<stdio.h>
#include<string.h>
const double EPS = 1e-5;
const int maxn = 1000 + 5;
int cnt, removed[maxn];
struct student
{char SID[15], name[15];int CID, score[4], sum, runk;double ave;
}stu[maxn];
void menu()
{printf("Welcome to Student Performance Management System (SPMS).\n\n""1 - Add\n""2 - Remove\n""3 - Query\n""4 - Show ranking\n""5 - Show Statistics\n""0 - Exit\n\n");
}
void Add()
{while (1){printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");scanf("%s", stu[cnt].SID);if (strcmp(stu[cnt].SID, "0") == 0)return;scanf("%d %s %d %d %d %d", &stu[cnt].CID, stu[cnt].name, &stu[cnt].score[0], &stu[cnt].score[1], &stu[cnt].score[2], &stu[cnt].score[3]);int flag = 0;for (int i = 0; i < cnt; i++)if (!removed[i] && strcmp(stu[i].SID, stu[cnt].SID) == 0){printf("Duplicated SID.\n");flag = 1;i = cnt - 1;break;}if (flag)continue;stu[cnt].sum = 0;for (int i = 0; i < 4; i++)stu[cnt].sum += stu[cnt].score[i];stu[cnt++].ave = stu[cnt].sum / 4.0 + EPS;}
}
void Remove()
{while (1){printf("Please enter SID or name. Enter 0 to finish.\n");int num = 0; char temp[15]; scanf("%s", temp);if (strcmp(temp, "0") == 0)return;for (int i = 0; i < cnt; i++)if (!removed[i] && (strcmp(stu[i].SID, temp) == 0 || strcmp(stu[i].name, temp) == 0)){removed[i] = 1;num++;}printf("%d student(s) removed.\n", num);}
}
void Query()
{while (1){printf("Please enter SID or name. Enter 0 to finish.\n");char temp[15]; scanf("%s", temp);if (strcmp(temp, "0") == 0)return;for (int i = 0; i < cnt; i++)if (!removed[i] && (strcmp(stu[i].SID, temp) == 0 || strcmp(stu[i].name, temp) == 0)){stu[i].runk = 1;for (int j = 0; j < cnt; j++)if (!removed[j] && stu[j].sum > stu[i].sum)stu[i].runk++;printf("%d %s %d %s %d %d %d %d %d %.2lf\n", stu[i].runk, stu[i].SID, stu[i].CID, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], stu[i].sum, stu[i].ave);}}
}
void Show()
{printf("Please enter class ID, 0 for the whole statistics.\n");int id, num = 0, passed[4] = { 0 }, overall[5] = { 0 }, All[4] = { 0 };double average[4] = { 0 };scanf("%d", &id);for (int i = 0; i < cnt; i++)if (!removed[i] && (stu[i].CID == id || id == 0)){num++; int ka = 0;for (int j = 0; j < 4; j++)All[j] += stu[i].score[j];if (stu[i].score[0] >= 60)passed[0]++, ka++;if (stu[i].score[1] >= 60)passed[1]++, ka++;if (stu[i].score[2] >= 60)passed[2]++, ka++;if (stu[i].score[3] >= 60)passed[3]++, ka++;for (int j = 4; j >= 1; j--)if (ka >= j)overall[j]++;if (!ka)overall[0]++;}for (int i = 0; i < 4; i++)average[i] = 1.0 * All[i] / num + EPS;printf("Chinese\nAverage Score: ");if (num)printf("%.2lf\n", average[0]); else printf("-nan\n");printf("Number of passed students: %d\n""Number of failed students: %d\n\n",passed[0], num - passed[0]);printf("Mathematics\nAverage Score: ");if (num)printf("%.2lf\n", average[1]); else printf("-nan\n");printf("Number of passed students: %d\n""Number of failed students: %d\n\n",passed[1], num - passed[1]);printf("English\nAverage Score: ");if (num)printf("%.2lf\n", average[2]); else printf("-nan\n");printf("Number of passed students: %d\n""Number of failed students: %d\n\n",passed[2], num - passed[2]);printf("Programming\nAverage Score: ");if (num)printf("%.2lf\n", average[3]); else printf("-nan\n");printf("Number of passed students: %d\n""Number of failed students: %d\n\n",passed[3], num - passed[3]);printf("Overall:\n""Number of students who passed all subjects: %d\n""Number of students who passed 3 or more subjects: %d\n""Number of students who passed 2 or more subjects: %d\n""Number of students who passed 1 or more subjects: %d\n""Number of students who failed all subjects: %d\n\n",overall[4], overall[3], overall[2], overall[1], overall[0]);
}
int main(void)
{while (1){int op; menu();scanf("%d", &op);if (op == 0)break;else if (op == 1)Add();else if (op == 2)Remove();else if (op == 3)Query();else if (op == 4)printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");else if (op == 5)Show();}return 0;
}
这篇关于例题4-6 师兄帮帮忙(A Typical Homework (a.k.a Shi Xiong Bang Bang Mang),Rujia Liu's Present 5, UVa 12412)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!