PAT 解题报告 1036. Boys vs Girls (25)

1036. Boys vs Girls 题目描述:

题目是说给定所有男学生(Boys)和女学生(Girls)的成绩记录, 要求找到分数最低的男学生和女学生输出相关信息, 若是其中某一个信息缺失, 就输出Absent和NA. 详见原题.

1036. Boys vs Girls 算法分析:

水题, 可能要自己加点判断信息缺失什么的, 直接模拟就最大最小即可. 代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <iomanip>

using namespace std;

typedef struct {
    char name[20];
    char gender[10];
    char ID[20];
    int grade;
    int flag; //0: undefined
} Record;


Record girl;
Record boy;

int N;
Record curr;
int main(void) {
    scanf("%d", &N);
    if(0 == N) return 0;
    girl.flag = 0;
    boy.flag = 0;
    while(N--) {
       scanf("%s", curr.name);
       scanf("%s", curr.gender);
       scanf("%s", curr.ID);
       scanf("%d", &curr.grade);
       curr.flag = 1;
       if('M' == curr.gender[0]) {
            if(boy.flag) {
                if(curr.grade < boy.grade)
                    memcpy(&boy, &curr, sizeof(Record));
            }
            else
                memcpy(&boy, &curr, sizeof(Record));
       }
       else {
            if(girl.flag) {
                if(curr.grade > girl.grade)
                    memcpy(&girl, &curr, sizeof(Record));
            }
            else
                memcpy(&girl, &curr, sizeof(Record));
       }
    }
    if(girl.flag && boy.flag) {
        cout<<girl.name<<" "<<girl.ID<<endl;
        cout<<boy.name<<" "<<boy.ID<<endl;
        cout<<girl.grade-boy.grade<<endl;
    }
    else {
        if(girl.flag)
            cout<<girl.name<<" "<<girl.ID<<endl;
        else
            cout<<"Absent"<<endl;
        if(boy.flag)
            cout<<boy.name<<" "<<boy.ID<<endl;
        else
            cout<<"Absent"<<endl;
        cout<<"NA"<<endl;
    }
    return 0;
}

1036. Boys vs Girls 注意点:

无.

 

(全文完,原创文章,转载时请注明作者和出处)


(转载本站文章请注明作者和出处 烟客旅人 sigmainfy — http://www.sigmainfy.com,请勿用于任何商业用途)

Written on December 28, 2013