Content
一次考試中,於所有及格學生中獲取最低分數者最為幸運,反之,於所有不及格同學中,獲取最高分數者,可以說是最為不幸,而此二種分數,可以視為成績指標。
請你設計一支程式,讀入全班成績(人數不固定),請對所有分數進行排序,並分別找出不及格中最高分數,以及及格中最低分數。
當找不到最低及格分數,表示對於本次考試而言,這是一個不幸之班級,此時請你印出「worst case」;反之,當找不到最高不及格分數時,請你印出「best case」。
( 註:假設及格分數為 60 )。
Input
第一行輸入學生人數,第二行為各學生分數(0~100 間),分數與分數之間以一個空白間格。
每一筆測資的學生人數為 1~20 的整數。
Output
每筆測資輸出三行。
第一行由小而大印出所有成績,兩數字之間以一個空白間格,最後一個數字後無空白;
第二行印出最高不及格分數,如果全數及格時,於此行印出 best case ;
第三行印出最低及格分數,如果全數不及格時,於此行印出 worst case 。
Sample Input #1
10 0 11 22 33 55 66 77 99 88 44
Sample Output #1
0 11 22 33 44 55 66 77 88 99 55 66
Sample Input #2
1 13
Sample Output #2
13 13 worst case
Sample Input #3
2 73 65
Sample Output #3
65 73 best case 65
CPP
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,score[20],a60/*最低分及格成績*/,b60/*最高分不及格成績*/;
while(cin>>n){ //n=人數
a60=-1,b60=-1;
for(int i=0;i<n;i++){ //等於py中的int(input().split())
cin>>score[i];
}
sort(score,score+n);//排序
for(int j=0;j<n;j++){
if(score[j]<60){
b60=score[j];
}
}
for(int k=n-1;k>=0;k--){
if(score[k]>=60){
a60=score[k];
}
}
cout<<score[0];
for(int l=1;l<n;l++){
cout<<" "<<score[l];
}
cout<<endl;
if (b60!=-1)cout<<b60<<endl;
else cout<<"best case"<<endl;
if (a60!=-1)cout<<a60<<endl;
else cout<<"worst case"<<endl;
}
return 0;
}
PYTHON
from sys import stdin
n=int(stdin.readline())
s=[int(x) for x in stdin.readline().split()]
s.sort(key=lambda x:x)
print(*s)
b=[x for x in s if x<60]
if len(b)==0:
print('best case')
else:
print(b[-1])
a=[x for x in s if x>=60]
if len(a)==0:
print('worst case')
else:
print(a[0])
文章標籤
全站熱搜
