您好,欢迎访问三七文档
编程实战大奖赛某大奖赛有n(n=1)个选手参赛,m(m2)个评委为选手打分(0~10)。统分规则为:在每个选手的m个得分中,去掉一个最高分和一个最低分,取平均分为选手最终得分。编程实现:(1)根据n个选手的最终得分,从高到低排定名次,前3名获得金、银、铜牌。(2)根据每个选手的最终得分计算每个评委为该选手打分的偏差,为全部选手所打分数的偏差之和作为该评委的得分,将所有评委的得分从低到高排序,第一名获得最佳评委奖。分析首先,大致描述程序流程输入选手得分计算选手最终得分计算评委得分计算评委最终得分选手排名评委排名打印获奖选手名单打印获奖评委名单输入选手名单输入评委名单分析然后,分析程序要处理的数据选手用编号表示,为一个4位整数(1001~9999)评委也用编号,用3位整数(101~999)表示评委选手118158388565898…选手得分1234888.596…8.45234577.5868…7.68345699989…9……………………分析然后,分析程序要处理的数据选手用编号表示,为一个4位整数(1001~9999)评委也用编号,用3位整数(101~999)表示评委选手118158388565898…12340.450.450.050.552.45…23450.680.180.321.680.32…345600010……………………评委得分4.573.551.354.657.84设计首先,设计出主要数据结构选手相关编号:iPlayerID[N]原始分:fPlayerScore1[N][M]最终得分:fPlayerScore2[N]评委相关编号:iJudgeID[M]原始分:fJudgeScore1[N][M]最终得分:fJudgeScore2[M]设计其次,划分程序模块,细化程序流程把数据结构映射到每个模块输入选手编号:InputPlayerID(iPlayerID[N])输入评委编号:InputJudgeID(iJudgeID[M])输入选手原始分:InputPlayer(fPlayerScore1[N][M])计算选手最终得分:PlayerScore(fPlayerScore1[N][M],fPlayerScore2[N])选手排名:sortPlayer(iPlayerID[N],fPlayerScore2[N])计算评委原始分:JudgeScore1(fPlayerScore1[N][M],fPlayerScore2[N],fJudgeScore1[N][M])计算评委最终得分:JudgeScore(fJudgeScore1[N][M],fJudgeScore2[M])评委排名:sortJudge(iJudgeID[M],fJudgeScore2[M])打印选手排名:printPlayer(iPlayerID[N],fPlayerScore2[N])打印评委排名:printJudge(iJudgeID[M],fJudgeScore2[M])设计然后,对模块进行分析综合输入选手编号和评委编号可用一个函数实现简化为:inputID(id[])选手排名、评委排名也可合并为一个函数(flag为0表示从低到高,为1表示从高到低)简化为:sort(id[],score[],flag)两个计算最终得分的函数能不能合并为一个?不能!为什么?计算选手最终得分、评委原始分和评委最终得分3个函数关系密切,可合并,同时可减少传递评委原始分fJudgeScore[N][M]简化为:voidCalScore(fPlayerScore1[N][M],fPlayerScore2[],fJudgeScore2[M])设计再次,确认各模块的构成,并映射到程序流程输入编号:intInputID(intID[N])输入选手原始分:voidInputPlayer(floatfPlayerScore1[N][M])计算得分:voidcalScore(floatfPlayerScore1[N][M],floatfPlayerScore2[N],floatfJudgeScore2[M])排名:voidsort(intID[N],floatfScore[N],intflag)打印选手排名:voidprintPlayer(intiPlayerID[N],floatfPlayerScore2[N])打印评委排名:voidprintJudge(intiJudgeID[M],floatfJudgeScore2[M])inputplayer()calScore()sort(Player)sort(judge)printPlayer()printJudge()inputID(player)inputID(Judge)设计可尝试设计出初步的主函数main(){n=inputID(iPlayerID);m=inputID(iJudgeID);inputPlayer(fPlayerScore1);calScore(fPlayerScore1,fPlayerScore2,fJudgeScore2);sort(iPlayerID,fPlayerScore2,1);sort(iJudgeID,fJudgeScore2,0);printPlayer(iPlayerID,fPlayerScore2);printJudge(iJudgeID,fJudgeScore2);}inputplayer()calScore()sort(Player)sort(judge)printPlayer()printJudge()inputID(player)inputID(Judge)设计最后,设计程序的细节各函数的参数和返回值inputID函数返回人数n或m其他函数无返回值形参中的数组与选手有关的加参数n,与评委有关的加参数m算法calScore函数算法见统分规则sort函数:冒泡排序输入、输出格式请为nnnn选手打分:评委mmm打分:8评委mmm打分:8.5……选手排名编号得分名次奖牌nnnn101金牌nnnn9.52银牌nnnn9.453铜牌nnnn9.204……最佳裁判:mmm编码首先,编写主函数部分#includestdio.h#includestdlib.h#defineM10#defineN100intInputID(intID[],intflag);//flag为0代表选手,1代表评委voidInputPlayer(intiPlayerID[],intJudgeID[],floatfPlayerScore1[][M],intn,intm);voidcalScore(floatfPlayerScore1[][M],floatfPlayerScore[],floatfJudgeScore[],intn,intm);voidsort(intID[],floatfScore[],intn,intflag);voidprintPlayer(intiPlayerID[],floatfPlayerScore[],intn);voidprintJudge(intiJudgeID[],floatfJudgeScore[],intm);intmain(){intm,n,iPlayerID[N],iJudgeID[M];floatfPlayerScore1[N][M],fPlayerScore[N],fJudgeScore[M];n=inputID(iPlayerID,0);m=inputID(iJudgeID,1);inputPlayer(iPlayerID,iJudgeID,fPlayerScore1,n,m);calScore(fPlayerScore1,fPlayerScore,fJudgeScore,n,m);sort(iPlayerID,fPlayerScore,n,1);sort(iJudgeID,fJudgeScore,m,0);printPlayer(iPlayerID,fPlayerScore,n);printJudge(iJudgeID,fJudgeScore,m);return0;}编码其次,依次编写各函数intinputID(intid[],intflag){inti=0;if(flag==0)printf(请输入参赛选手编号(输入0结束):\n);elseprintf(请输入评委编号(输入0结束):\n);do{printf(%d:,i+1);scanf(%d,&id[i]);}while(id[i]!=0);returni;}voidprintJudge(intiJudgeID[],fJudgeScore[],intm){printf(**********************\n);printtf(最佳裁判:%d\n,iJudgeID[0]);printf(**********************\n);}编码voidiSwap(int*a,int*b){intt;t=*a;*a=*b;*b=t;}voidfSwap(float*a,float*b){floatt;t=*a;*a=*b;*b=t;}voidsort(intid[],floatScore[],intn,intflag){inti,j,c,iTemp;floatfTemp;for(i=0;in-1;i++){for(j=0;ji;j++){if(flag)c=score[j]score[j+1]elsec=score[j]score[j+1]if(c){iSwap(&Score[j],&Score[j+1]);fSwap(&ID[j],&ID[j+1]);}}}}编码voidcalScore(floatfPlayerScore1[][M],floatfPlayerScore[],floatfJudgeScore[],intn,intm){inti,j;floatfMin,fMax,fSum;for(i=0;in;i++){fMin=fMax=fSum=fPlayerScore1[0];for(j=1;jm;j++){fSum+=fPlayerScore1[i][j];if(fMinfPlayerScore1[i][j])fMin=fPlayerScore1[i][j];if(fMaxfPlayerScore1[i][j])fMax=fPlayerScore1[i][j];}fPlayerScore[i]=(fSum-fMin-fMax)/(m-2);}for(j=0;jm;j++){fJudgeScore[j]=0;for(i=0;in;i++)fJudgeScore[j]+=fabs(fPlayerScore1[i][j]-fPlayerScore[i]);}}测试静态测试:尝试修复所有语法错误字面错误编译错误连接错误动态测试:发现并修复运行时错误选择多种输入数据尝试各种操作方式为进一步查找错误,可更换IDE(如VC、CB)优化可根据情况对程序进行必要的优化数据优化:输入数据有效性选手(评委)编号评委打分(最多1位小数)计算数据有效性最终得分只保留2位小数算法优化:排序算法的选择名次并列时如何处理?编程实战:计算学分绩点某大学用学分绩点(GPA)评定学生的学业成绩。学分绩点计算公式为:GPA=∑(课程绩点X课程学分)/∑课程学分,其中课程绩点是从课程的百分制成绩转换得到的,转换方法见下表。试编程统计某班n个同学在m门必修课上的学分绩点,并按从高到低排序。百分制课程成绩对应的绩点课程绩点43.73.332.72.321.71.510课程成绩≥90≥85≥82≥78≥75≥72≥68≥66≥62≥6060
本文标题:编程实战(1)
链接地址:https://www.777doc.com/doc-3791389 .html