zoukankan      html  css  js  c++  java
  • A1036 Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

    Input Specification:

    Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's namegenderID and grade, separated by a space, where name and IDare strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

    Output Specification:

    For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF​​gradeM​​. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

    Sample Input 1:

    3
    Joe M Math990112 89
    Mike M CS991301 100
    Mary F EE990830 95
    
     

    Sample Output 1:

    Mary EE990830
    Joe Math990112
    6
    
     

    Sample Input 2:

    1
    Jean M AA980920 60
    
     

    Sample Output 2:

    Absent
    Jean AA980920
    NA
    
     

    思路:

    开两个结构体动态数组,boy(n)和girl(n),然后分别排序输出最小和最大;输出时考虑Absent和NA情况。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <vector>
     5 using namespace std;
     6 struct Students
     7 {
     8     string name;
     9     char gender;
    10     string id;
    11     int grade;
    12 };
    13 bool cp(Students a, Students b) {
    14     return a.grade < b.grade;
    15 }
    16 int main() {
    17     int n;
    18     cin >> n;
    19     string namei, idi;
    20     char genderi;
    21     int gradei;
    22     vector<Students>boy(n);
    23     vector<Students>girl(n);
    24     int j = 0, k = 0;
    25     for (int i = 0; i < n; i++) {
    26         cin >> namei >> genderi >> idi >> gradei;
    27         if (genderi == 'M') {
    28             boy[j].name = namei;
    29             boy[j].gender = genderi;
    30             boy[j].id = idi;
    31             boy[j].grade = gradei;
    32             j++;
    33         }
    34         else {
    35             girl[k].name = namei;
    36             girl[k].gender = genderi;
    37             girl[k].id = idi;
    38             girl[k].grade = gradei;
    39             k++;
    40         }    
    41     }
    42     sort(boy.begin(), boy.begin()+j, cp);
    43     sort(girl.begin(), girl.begin()+k, cp);
    44     if (k == 0)cout << "Absent" << endl;
    45     else cout << girl[k - 1].name << " " << girl[k - 1].id<<endl;
    46     if (j == 0)cout << "Absent" << endl;
    47     else cout << boy[0].name << " " << boy[0].id << endl;
    48     if (j == 0 || k == 0)cout << "NA";
    49     else cout << girl[k - 1].grade - boy[0].grade;
    50     return 0;
    51 }
    作者:PennyXia
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    bzoj 2442: [Usaco2011 Open]修剪草坪【单调栈】
    bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级【分层图+spfa】
    bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】
    bzoj 1677: [Usaco2005 Jan]Sumsets 求和【dp】
    bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】
    洛谷 P4180 【模板】严格次小生成树[BJWC2010]【次小生成树】
    bzoj 1660: [Usaco2006 Nov]Bad Hair Day 乱发节【单调栈】
    bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路【dijskstra】
    bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】
    bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】
  • 原文地址:https://www.cnblogs.com/PennyXia/p/12288276.html
Copyright © 2011-2022 走看看