1. 程式人生 > >UVA - 12412,A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

UVA - 12412,A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

題巨長,但是不難,一個bug調了好長時間,終於ac了。浮點數加上1e-5;當資料庫空的時候直接輸出0.00(雖然udebug裡是輸出nan);還有選操作4時輸出的單引號應該是英文的,樣例給的是中文的。

傳送門:UVA-12412

AC程式碼:
 

#include<iostream>
#include<map>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const double eps=1e-5;
struct student{
	int cla,pos,ma,en,ch,co,all,rank;//cla代表班級,ma表示數學,en表示英語,ch表示語文,co表示程式設計,all表示總分,rank表示排名,pos表示進入庫的順序
	double ave;//均分
	string name,num;//num表示編號
};
int all_cnt;
map<string,student> pq;//按編號儲存資料
vector<student> op;
//按進入先後排序
bool cmp1 (student a,student b){
	return a.pos<b.pos;
}
//按分數排序
bool cmp2 (student a,student b){
	return a.all>b.all;
}
void start()
{
	printf("Welcome to Student Performance Management System (SPMS).\n\n");
	printf("1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n");
}
void op1()
{
	string tmp;
	printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
	while(cin>>tmp){
		if(tmp=="0") break;
		student yu;
		cin>>yu.cla>>yu.name>>yu.ch>>yu.ma>>yu.en>>yu.co;
		yu.all=yu.ch+yu.co+yu.en+yu.ma;yu.ave=yu.all*1.0/4;
		yu.pos=all_cnt++;yu.num=tmp;
		if(pq.count(tmp)){
			printf("Duplicated SID.\n");
			printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
			continue;
		}
		pq[tmp]=yu;
		op.push_back(yu);
		printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
	}
}
void op2()
{
	string tmp;
	printf("Please enter SID or name. Enter 0 to finish.\n");
	map<string,student>::iterator it;
	while(cin>>tmp){
		if(tmp=="0") break;
		int cnt=0;
		//在map裡刪除
                for(it=pq.begin();it!=pq.end();){
			student &gh=it->second;
			if(tmp==gh.name||tmp==gh.num) ++cnt,pq.erase(it++);
			else ++it;
		}
		printf("%d student(s) removed.\n",cnt);
		printf("Please enter SID or name. Enter 0 to finish.\n");
	}
        //同時在vector裡刪除相應資料
	vector<int> kl;
	for(int i=0;i<op.size();++i) if(!pq.count(op[i].num)) kl.push_back(i);
	for(int i=0;i<kl.size();++i) op.erase(op.begin()+kl[i]-i);
}
void op3()
{
	string tmp;
	int point=-1,cnt=1,last=0;
	vector<student>::iterator it;
	printf("Please enter SID or name. Enter 0 to finish.\n");
	sort(op.begin(),op.end(),cmp2);//按分數排序
	//標定排名
        for(int i=0;i<op.size();++i){
		if(op[i].all==point) op[i].rank=last;
		else op[i].rank=cnt,last=cnt,point=op[i].all;
		++cnt;
		pq[op[i].num]=op[i];
	}
	while(cin>>tmp){
		if(tmp=="0") break;
		vector<student> rt;
		for(int i=0;i<op.size();++i)
			if(op[i].num==tmp||op[i].name==tmp) rt.push_back(op[i]);
		sort(rt.begin(),rt.end(),cmp1);//將應輸出的學生按進入順序排序
		for(int i=0;i<rt.size();++i){
			student gh=rt[i];
			printf("%d %s %d %s %d %d %d %d %d %.2lf\n",
			gh.rank,gh.num.c_str(),gh.cla,gh.name.c_str(),gh.ch,gh.ma,gh.en,gh.co,gh.all,gh.ave+eps);
		}
		printf("Please enter SID or name. Enter 0 to finish.\n");
	} 
}
void op5()
{
	printf("Please enter class ID, 0 for the whole statistics.\n");
	int n,cnt=0,yu[10];
	double jk[5][5];
	cin>>n;
	memset(jk,0,sizeof(jk));memset(yu,0,sizeof(yu));
	for(map<string,student>::iterator it=pq.begin();it!=pq.end();++it){
		student &gh=it->second;
		if(!n||n&&gh.cla==n){
			int fg=0;
			jk[0][0]+=gh.ch;jk[1][0]+=gh.ma;jk[2][0]+=gh.en;jk[3][0]+=gh.co;
			if(gh.ch>=60) ++fg,++jk[0][1];else ++jk[0][2];
			if(gh.ma>=60) ++fg,++jk[1][1];else ++jk[1][2];
			if(gh.en>=60) ++fg,++jk[2][1];else ++jk[2][2];
			if(gh.co>=60) ++fg,++jk[3][1];else ++jk[3][2];
			++cnt;
			if(!fg) ++yu[0];
			else for(int i=1;i<=4;++i) if(fg>=i) ++yu[i];
		}
	}
	string io[4]={"Chinese","Mathematics","English","Programming"};
	string hj[3]={"Average Score: ","Number of passed students: ","Number of failed students: "};
	for(int i=0;i<4;++i){
		jk[i][0]+=eps;
		if(cnt) jk[i][0]/=(1.0*cnt);
		else jk[i][0]=0;
	} 
	for(int i=0;i<4;++i){
		printf("%s\n",io[i].c_str());
		for(int j=0;j<3;++j)
			if(!j) printf("%s%.2f\n",hj[j].c_str(),jk[i][j]+eps);
			else printf("%s%d\n",hj[j].c_str(),int(jk[i][j])); 
		cout<<endl;
	}
	printf("Overall:\n");
	printf("Number of students who passed all subjects: %d\n",yu[4]);
	printf("Number of students who passed 3 or more subjects: %d\n",yu[3]);
	printf("Number of students who passed 2 or more subjects: %d\n",yu[2]);
	printf("Number of students who passed 1 or more subjects: %d\n",yu[1]);
	printf("Number of students who failed all subjects: %d\n\n",yu[0]);
}
int main()
{
	int n;
	start();
	all_cnt=0;
	while(cin>>n){
		if(!n) break;
		if(n==1) op1();
		else if(n==2) op2();
		else if(n==3) op3();
		else if(n==4) printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
		else if(n==5) op5();
		start();
	}
	return 0;
}