1. 輸出低於班級平均分的學生信息
【問題描述】
輸入N個學生的姓名和語文、數學的得分,求平均分低於班級平均分的學生,將其信息全部輸出。分數相同的按輸入先后輸出。
輸入格式:第1行,有一個整數N,N的范圍是[1…100];下面有N行,每行一個姓名(字符串),2個整數。姓名由不超過10個的小寫字母組成,整數范圍是[0…100]。
輸出格式:平均分低於班級平均分的學生信息。格式:姓名 語文 數學 平均分。每個學生占一行。如果沒有,輸出none。
【樣例輸入】
4
gaoxiang 78 96
wangxi 70 99
liujia 90 87
zhangjin 78 73
【樣例輸出】
zhangjin 78 73 75.5
#include<iostream> #include<string> using namespace std; struct student { string name; int chinese,math; double avg; }; student stu[101]; double total = 0; int main() { int n; cin >> n; for(int i=0; i<n; i++) { cin >> stu[i].name >> stu[i].chinese >> stu[i].math; stu[i].avg = (stu[i].chinese + stu[i].math)/2.0; total = total + stu[i].chinese + stu[i].math; } double classAvg = total / (n*2); bool flag = false; for(int i=0; i<n; i++) { if(stu[i].avg < classAvg) { cout << stu[i].name << " "<<stu[i].chinese << " "<< stu[i].math << " "<< stu[i].avg<<endl; flag = true; } } if(flag == false){ cout << "none"<<endl; } return 0; }
2. 誰拿了最多獎學金
【問題描述】
某校的慣例是在每學期的期末考試之后發放獎學金。發放的獎學金共有五種,獲取的條件各自不同:
1)院士獎學金,每人8000元,期末平均成績高於80分(>80),並且在本學期內發表1篇或1篇以上論文的學生均可獲得;
2)五四獎學金,每人4000元,期末平均成績高於85分(>85),並且班級評議成績高於80分(>80)的學生均可獲得;
3)成績優秀獎,每人2000元,期末平均成績高於90分(>90)的學生均可獲得;
4)西部獎學金,每人1000元,期末平均成績高於85分(>85)的西部省份學生均可獲得;
5)班級貢獻獎,每人850元,班級評議成績高於80分(>80)的學生干部均可獲得;
只要符合條件就可以得獎,每項獎學金的獲獎人數沒有限制,每名學生也可以同時獲得多項獎學金。例如姚林的期末平均成績是87分,班級評議成績82分,同時他還是一位學生干部,那么他可以同時獲得五四獎學金和班級貢獻獎,獎金總數是4850元。
現在給出若干學生的相關數據,請計算哪些同學獲得的獎金總數最高(假設總有同學能滿足獲得獎學金的條件)。
輸入
第一行是一個整數N(1 <= N <= 100),表示學生的總數。接下來的N行每行是一位學生的數據,從左向右依次是姓名,期末平均成績,班級評議成績,是否是學生干部,是否是西部省份學生,以及發表的論文數。姓名是由大小寫英文字母組成的長度不超過20的字符串(不含空格);期末平均成績和班級評議成績都是0到100之間的整數(包括0和100);是否是學生干部和是否是西部省份學生分別用一個字符表示,Y表示是,N表示不是;發表的論文數是0到10的整數(包括0和10)。每兩個相鄰數據項之間用一個空格分隔。
輸出
包括三行,第一行是獲得最多獎金的學生的姓名,第二行是這名學生獲得的獎金總數。如果有兩位或兩位以上的學生獲得的獎金最多,輸出他們之中在輸入文件中出現最早的學生的姓名。第三行是這N個學生獲得的獎學金的總數。
【樣例輸入】
4
YaoLin 87 82 Y N 0
ChenRuiyi 88 78 N Y 1
LiXin 92 88 N N 0
ZhangQin 83 87 Y N 1
【樣例輸出】
ChenRuiyi
9000
28700
#include<iostream> #include<algorithm> using namespace std; struct student { string name; int avg,classgrade,paper,total,m; char gb,xb; }; student stu[101]; bool comp(student a,student b) { return a.total > b.total; } int main(){ int n,sum=0; cin >> n; for(int i=0; i<n; i++) { stu[i].total = 0; cin >>stu[i].name >> stu[i].avg >> stu[i].classgrade>>stu[i].gb >> stu[i].xb >> stu[i].paper; if(stu[i].avg>80 && stu[i].paper >= 1) { stu[i].total += 8000; } if(stu[i].avg>85 && stu[i].classgrade>80 ) { stu[i].total +=4000; } if(stu[i].avg>90) { stu[i].total +=2000; } if(stu[i].avg>85 && stu[i].xb=='Y' ) { stu[i].total +=1000; } if(stu[i].avg>80 && stu[i].gb=='Y') { stu[i].total += 850; } sum += stu[i].total; } // sort(stu,stu+n,comp); cout << stu[0].name << endl; cout << stu[0].total <<endl; cout << sum <<endl; return 0; }
1. 窗口重疊
【問題描述】
窗口重疊。在Windows操作系統中,最主要的桌面元素是窗口。通常一個窗口有4個整數定義位置:左邊坐標(left)、右邊坐標(right)、上邊坐標(top)、下邊坐標(bottom)。
現在給你兩個窗口位置信息,判斷它們位置是否有重疊。
輸入格式:共2行,每行四個整數,表示一個窗口的位置信息。
輸出格式:如果兩個窗口位置重疊,輸出重疊的面積;否則輸出0。
【樣例輸入】
10 100 20 60
60 160 50 200
【樣例輸出】
400
#include<iostream> #include<string> using namespace std; struct Window { int left,right,top,bottom; }; Window WinA,WinB,temp; //定義一個函數輸入窗口變量 Window inDate() { Window temp; cin >> temp.left >> temp.right >> temp.top >> temp.bottom; return temp; } int main() { WinA = inDate(); WinB = inDate(); //判斷計算重疊窗口temp temp.left = max(WinA.left,WinB.left); temp.right = min(WinA.right,WinB.right); temp.top = max(WinA.top,WinB.top); temp.bottom = min(WinA.bottom,WinB.bottom); int s = (temp.right - temp.left)*(temp.bottom - temp.top ); if((temp.right<=temp.left)||(temp.bottom <= temp.top)) { s = 0; } cout << s << endl; return 0; }
2. 第K名
【問題描述】
剛舉行的萬米長跑活動中,有N個人跑完了全程,所用的時間都不相同。頒獎時為了增加趣味性,隨機抽了一個數K,要獎勵第K名一雙跑鞋。
現在組委會給你N個人的姓名、成績(用時,單位是秒),請你編程快速輸出第K名的姓名。
輸入
第一行:2個整數N和K,范圍(1≤ K ≤ N ≤ 100)。
下面N行:每行第1個是字符串表示姓名;第2個是個整數,表示這個人跑完的使用時間。
輸出
一行,第K名的姓名。
【樣例輸入】
5 3
wangxi 2306
xiaoming 3013
zhangfan 3189
chengli 4012
jiangbou 2601
【樣例輸出】
xiaoming
#include<iostream> #include<algorithm> #include<string> using namespace std; struct competition{ string name; int time; }; competition pep[101]; bool comp(competition a,competition b){ return a.time < b.time; } int main() { int n,k; cin >> n >> k; for(int i=0;i<n;i++){ cin >> pep[i].name >> pep[i].time; } sort(pep,pep+n,comp); for(int i=0;i<n;i++){ if(i==k-1){ cout << pep[i].name; } } return 0; }