MOOC清华《程序设计基础》期末考试第3题:统计高考录取人数与录取最低分

来源:互联网 发布:网络诈骗的手法 编辑:程序博客网 时间:2024/05/16 02:02

题目描述

正值高考录取阶段,现在有一份考生志愿填报文件,文件名为datain.txt,文件中一共有三所候选大学,A,B和C,文件第一行分别为三所大学的录取人数上限,以空格隔开。

后面若干行(小于十万条)为具体的考生报考信息,具体格式为:考号 成绩 平行志愿1 平行志愿2。按照成绩从高到低的顺序排列(没有重分,满分750分)以下内容为datain.txt文件示例(仅仅是示例,考号和成绩均不具有参照性):

1 1 1

2015001 600 A B

2015002 580 A C

2015003 555 C B

2015004 500 A B

然后要求将A B C 三所大学的录取人数以及录取的最低分数写入一个文件名为dataout.txt的文件,内容为A大学最终录取人数,然后空格,然后录取的最低分数,然后空格,然后B大学录取人数,空格,录取最低分数,空格,C大学录取人数,空格,分数。

以上述题干为例,那么2015001可以被A大学顺利录取,2015002就无法进入A大学,只能被C大学录取,同理,2015003只能被B大学录取,2015004将无学可上。。所以写入dataout.txt文件的内容应该如下:

1 600 1 555 1 580

输入格式

输出格式

样例输入

见题目描述示例

样例输出

见题目描述示例

#include <iostream>#include <fstream>#include <cstring>  //用以包含memset函数 using namespace std;struct Student_t{int number;double score;char willing_1;char willing_2;};  //每个考生的报考信息 //本快速排序代码由刘聪先生原创,解决了全网快排区间都不是“左闭右开”的问题。//转载请注明出处。 void QuickSort(Student_t *array, int start, int end){//编程界的习惯是区间一律默认采用“左闭右开”的表达方式//即区间元素含有start而不含有end if(start >= end - 1) //设>=而不是==可以避免出错时出现死循环 return;  //经测试这里的条件判断关系表达式只能用>=,不能用==//说明start有可能出现越过end的情况,即比end大 int left = start, right = end;  //start、end都是不变量,所以需要设变量left、right Student_t pivot = array[left];Student_t temp;memset(&temp, 0, sizeof(temp));for(int i = left + 1; i < right;   )//循环从left + 1到right - 1,因为left要作为被比较元素 {if(array[i].score > array[left].score){temp = array[left];array[left] = array[i];array[i] = temp;left++;  //left跟着array[left]走 i++;  //循环变量 i 只在左端游标移动时才移动,右端游标移动时不变 }else{temp = array[i];for(int j = i; j < right; j++)array[j] = array[j + 1];  //最后一项array[right - 1]的值其实被越界元素array[right]赋值 array[right - 1] = temp;  //然后用缓存的array[i]的值覆盖掉错误的array[right - 1]right--; }}array[left] = pivot;QuickSort(array, start, left);QuickSort(array, left + 1, end);}//输出模块是测试代码 void OutputOfArray(Student_t *array, int n)  {      for(int i = 0; i < n; i++)          cout << array[i].number << ' ' << array[i].score << ' ' << array[i].willing_1 << ' ' << array[i].willing_2 << endl;  } int main(){int A_max, B_max, C_max;  //三所大学的录取人数上限int A_count = 0, B_count = 0, C_count = 0;  //报考每所大学的人数double A_score = 750, B_score = 750, C_score = 750; //每所大学的最低分 Student_t student[100000];int count = 0; //记录考生总数 ifstream fin("datain.txt");fin >> A_max >> B_max >> C_max; //读入三所大学录取人数上限 //cout << "A_max = " << A_max << ' ' << "B_max = " << B_max << ' ' //<< "C_max = " << C_max << endl;while(!fin.eof()){fin >> student[count].number >> student[count].score >> student[count].willing_1 >> student[count].willing_2;//cout << student[count].number << ' ' << student[count].score << ' '//<< student[count].willing_1 << ' ' << student[count].willing_2 << endl;count++;//cout << "count = " << count << endl;}//cout << "final count = " << count << endl; //final count == 18说明txt文件读完后还有一条空行读入 fin.close();QuickSort(student, 0, count);//OutputOfArray(student, count - 1); //测试代码,注意第二个实参是count - 1,不是count for(int i = 0; i < count - 1; i++){if((student[i].willing_1 == 'A')&&(A_count < A_max)){A_score = student[i].score;A_count++;}else if((student[i].willing_1 == 'B')&&(B_count < B_max)){B_score = student[i].score;B_count++;}else if((student[i].willing_1 == 'C')&&(C_count < C_max)){C_score = student[i].score;C_count++;}else if((student[i].willing_2 == 'A')&&(A_count < A_max)){A_score = student[i].score;A_count++;}else if((student[i].willing_2 == 'B')&&(B_count < B_max)){B_score = student[i].score;B_count++;}else if((student[i].willing_2 == 'C')&&(C_count < C_max)){C_score = student[i].score;C_count++;}}//遍历已从高到低排序且已读入内存中的考生名单,先按第一志愿录取,//若招生名额已满,则再考虑第二志愿。这里的六句话顺序不能错。 //cout << "A_count = " << A_count << ' ' << "A_score = " << A_score << ' '//<< "B_count = " << B_count << ' ' << "B_score = " << B_score << ' '//<< "C_count = " << C_count << ' ' << "C_score = " << C_score << endl;//测试代码,用cout语句测试输出ofstream fout("dataout.txt");fout << A_count << ' ' << A_score << ' ' << B_count << ' ' << B_score << ' '<< C_count << ' ' << C_score;fout.close();return 0;}

我自拟的测试文件如下:

//datain.txt3 2 42015001 600 A B2015002 580 A C2015003 555 C B2015004 500 A B2015005 400 A C2015006 413 B C2015007 499 A B2015008 512 C B2015009 524 B C2015010 521 C B2015011 619 B C2015012 627 A C2015013 571 B A2015014 534 A C2015015 591 A B2015016 599 B C2015017 687 A C

运行测试代码时测试结果如下:


最终运行题目要求代码时输出文件如下:

//dataout.txt3 600 2 599 4 524

Debug记录:以下语句
Student_t student[100000];
中,定义结构体数组下标为十万时,在我的电脑(Windows7、Core i5、4G内存)上运行时崩溃了,但是在学堂在线OJ系统中顺利通过评测;结构体数组下标为一万时,我的电脑上跑没问题,但是不符合题目要求“报考信息小于十万条”。

阅读全文
0 0
原创粉丝点击