【PAT乙】1080 MOOC期終成績 (25分)

problem

1080 MOOC期終成績 (25分)
對於在中國大學MOOC(http://www.icourse163.org/ )學習“數據結構”課程的學生,想要獲得一張合格證書,必須首先獲得不少於200分的在線編程作業分,然後總評獲得不少於60分(滿分100)。總評成績的計算公式爲 G=(G
​mid−term
​​ ×40%+G
​final
​​ ×60%),如果 G
​mid−term
​​ >G
​final
​​ ;否則總評 G 就是 G
​final
​​ 。這裏 G
​mid−term
​​ 和 G
​final
​​ 分別爲學生的期中和期末成績。

現在的問題是,每次考試都產生一張獨立的成績單。本題就請你編寫程序,把不同的成績單合爲一張。

輸入格式:
輸入在第一行給出3個整數,分別是 P(做了在線編程作業的學生數)、M(參加了期中考試的學生數)、N(參加了期末考試的學生數)。每個數都不超過10000。

接下來有三塊輸入。第一塊包含 P 個在線編程成績 G
​p
​​ ;第二塊包含 M 個期中考試成績 G
​mid−term
​​ ;第三塊包含 N 個期末考試成績 G
​final
​​ 。每個成績佔一行,格式爲:學生學號 分數。其中學生學號爲不超過20個字符的英文字母和數字;分數是非負整數(編程總分最高爲900分,期中和期末的最高分爲100分)。

輸出格式:
打印出獲得合格證書的學生名單。每個學生佔一行,格式爲:

學生學號 G
​p
​​ G
​mid−term
​​ G
​final
​​ G

如果有的成績不存在(例如某人沒參加期中考試),則在相應的位置輸出“−1”。輸出順序爲按照總評分數(四捨五入精確到整數)遞減。若有並列,則按學號遞增。題目保證學號沒有重複,且至少存在1個合格的學生。

輸入樣例:
6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81

輸出樣例:
missing 400 -1 99 99
ydjh2 200 98 82 88
dx86w 220 88 81 84
wehu8 300 55 84 84

solution

  • 知識:好多STL都忘記了,關於struct寫法,operator重載,st{}構造函數甚至是pair<string,st>定義, map轉vector, map排序, 元素(*it).second.gn訪問。
  • 思路:建立結構把學生信息存起來(gp,gm,gn,g),維護map判重(sring to int),轉成vector排個序。
  • 時間:題目挺長看完7mins,第一遍就寫了30mins+,然後錯了兩個點30mins,調試2次BUG,掛在四捨五入上。。綜上,思路都很簡單而且清楚,關鍵還是代碼能力,知識點不熟悉,以及調試用時長。
  • 我覺得我真的好久沒寫過用到這麼多奇奇怪怪語法的題目了
//W1. gp<200直接pass
//W2. 修改了四捨五入的方式 AC
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<cmath>
using namespace std;

struct st{
	string id;
	int gp, gm, gn, g; //double g;
	st(int x = -1){gp=gm=gn=x;g=0;}
	bool operator < (const st& b){
		if(this->g==b.g)return this->id<b.id;
		return this->g>b.g;
	}
};
bool cmp(pair<string,st> a,pair<string,st> b){return a.second < b.second;}

int main(){
	int p, m, n;
	cin>>p>>m>>n;
	map<string,st>ans;
	for(int i = 1; i <= p; i++){
		string a; int b; cin>>a>>b;
		st c; c.id = a; c.gp=b;
		if(b>=200)ans.insert(make_pair(a,c));
	}
	for(int i = 1; i <= m; i++){
		string a; int b; cin>>a>>b;
		if(ans.count(a))ans[a].gm = b;
	}
	for(int i = 1; i <= n; i++){
		string a; int b; cin>>a>>b;
		if(ans.count(a))ans[a].gn = b;
	}
	for(map<string,st>::iterator it = ans.begin(); it != ans.end(); it++){
		/*
		if((*it).second.gn>(*it).second.gm)(*it).second.g = (*it).second.gn;
		else (*it).second.g = 0.4*(*it).second.gm+0.6*(*it).second.gn;
		*/
		if((*it).second.gn>(*it).second.gm)(*it).second.g = (*it).second.gn;
		else (*it).second.g = int(0.4*(*it).second.gm+0.6*(*it).second.gn+0.5);
	}
	vector<pair<string,st> >aa(ans.begin(),ans.end());
	sort(aa.begin(),aa.end(),cmp);
	for(vector<pair<string,st> >::iterator it = aa.begin(); it != aa.end(); it++){
		if((*it).second.g>=60)
			printf("%s %d %d %d %d\n",(*it).second.id.c_str(), (*it).second.gp, (*it).second.gm, (*it).second.gn,(*it).second.g);
		//(int)round((*it).second.g)
	}
	return 0;
}
//W2.修改了四捨五入的方式
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<cmath>
using namespace std;

struct st{
	string id;
	int gp, gm, gn, g;
	int ok;
	st(int x = -1){gp=gm=gn=ok=x;g=0;}
	bool operator < (const st& b){
		if(this->g==b.g)return this->id<b.id;
		return this->g>b.g;
	}
};
bool cmp(pair<string,st> a,pair<string,st> b){return a.second < b.second;}

int main(){
	int p, m, n;
	cin>>p>>m>>n;
	map<string,st>ans;
	for(int i = 1; i <= p; i++){
		string a; int b; cin>>a>>b;
		st c; c.id = a; c.gp=b;
		if(!ans.count(a))ans[a] = c;//ans.insert(make_pair(a,c));
	}
	for(int i = 1; i <= m; i++){
		string a; int b; cin>>a>>b;
		st c; c.id = a; c.gm=b;
		if(!ans.count(a))ans.insert(make_pair(a,c));
		else ans[a].gm = b;
	}
	for(int i = 1; i <= n; i++){
		string a; int b; cin>>a>>b;
		st c; c.id = a; c.gn=b;
		if(!ans.count(a))ans.insert(make_pair(a,c));
		else ans[a].gn = b;
	}
	for(map<string,st>::iterator it = ans.begin(); it != ans.end(); it++){
		if((*it).second.gn>(*it).second.gm)(*it).second.g = (*it).second.gn;
		else (*it).second.g = int(0.4*(*it).second.gm+0.6*(*it).second.gn+0.5);
	}
	vector<pair<string,st> >aa(ans.begin(),ans.end());
	sort(aa.begin(),aa.end(),cmp);
	for(vector<pair<string,st> >::iterator it = aa.begin(); it != aa.end(); it++){
		if((*it).second.gp>=200 && (*it).second.g>=60)
			printf("%s %d %d %d %d\n",(*it).second.id.c_str(), (*it).second.gp, (*it).second.gm, (*it).second.gn,(*it).second.g);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章