j605. 程式考試

2023.1.9 PM 09:30 by CBJ

來源 : https://zerojudge.tw/ShowProblem?problemid=j605
出題者 : 2023年1月APCS
標籤 : 迴圈、找最大值
難易度 : 1
解題想法 : 
依照題意實作即可,可以邊輸入邊做,也可以全部讀完後再用另一個迴圈。

※特別注意 : 假如最終成績小於0要輸出0
//C language

#include<stdio.h>
#define to_pos(ans) ((ans>=0)?ans:0)
int main(){
    int k,max_score=-1,times=0,wrong=0;
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        if(b>max_score) times=a, max_score=b;
        if(b==-1) wrong++;
    }
    int ans=max_score-k-(wrong*2);
    printf("%d %d\n",to_pos(ans),times);
    return 0;
}
//C++ language

#include<iostream>
#include<algorithm> //max()
using namespace std;
int main(){
    int k; cin>>k;
    int max_score=-1,times=0,wrong=0;
    for(int i=0;i<k;i++){
        int a,b; cin>>a>>b;
        if(b>max_score) times=a, max_score=b;
        if(b==-1) wrong++;
    }
    int ans=max_score-k-(wrong*2);
    cout<<max(0,ans)<<" "<<times<<"\n";
    return 0;
}
## Python language

k=int(input())
max_score=-float('inf')
times=wrong=0
for i in range(k):
    a,b=map(int,input().split())
    if b>max_score:
        times=a 
        max_score=b 
    if b==-1: wrong+=1 
ans=max_score-k-(wrong*2)
print(max(0,ans),times)

相關文章