2019-08-07 A1036

又是同一类型的题目但是却又没有轻易地ac,这个原因我至今没找到,应该是输出部分的代码有问题,有一个测试点一直显示答案错误,换成柳神的输出代码便ac了。

#include 
using namespace std; 
int main() {
    int n, grade;
    string name, id, hname, lname, hid, lid, gender;
    int max = -1, min = 101;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        cin >> name >> gender >> id;
        scanf("%d", &grade);
        if(gender == "F"){
            if(grade > max){
            max = grade;
            hname = name;
            hid = id;   
            }
        }
        else if(gender == "M"){
            if(grade < min){
                min = grade;
                lname = name;
                lid = id;
            }
        }
        
    }
    if(max != -1)
        cout << hname << " " << hid << endl;
    else
        printf("Absent\n");
    if(min != 101)
        cout << lname << " " << lid << endl;
    else
        printf("Absent\n");
    if(max != -1 && min != 101)
        printf("%d", max - min);
    else
        printf("NA");

    return 0;
}

之前我的输出部分的代码是这样的:

    if(max == -1 )
        cout << "Absent" << endl << lname << " " << lid << endl << "NA";
    else if(min == 101)
        cout << "Absent" << endl << hname << " " << hid << endl << "NA";
    else if(max != -1 && min != 101){
        cout << hname<< " " << hid << endl;
        cout << lname << " " << lid << endl << max - min;
    }

不过这段代码看上去就啰嗦得很一点也不简洁。

你可能感兴趣的:(2019-08-07 A1036)