当前位置: 首页 > news >正文

C++笔记(1)——Anniversary

世界太喧闹,不如敲代码。

直接上题目:


Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID's of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.

Input Specification:
Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer N (≤10的5次方 ). Then N lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X. It is guaranteed that all the ID's are distinct.

The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer M (≤10的5次方 ). Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID's are distinct.

Output Specification:
First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus -- notice that the 7th - 14th digits of the ID gives one's birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.

Sample Input:
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042

Sample Output:
3
150702193604190912


说白了就是给两组名单,一组是来访校友,一组是真正来了的嘉宾,要求输出最老的来访校友的ID,如果没有校友那么输出最老嘉宾的名单,同时还要输出来访的校友的总人数。

代码:

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(string a, string b) {
    return a.substr(6, 8) < b.substr(6, 8); // 就是直接比较字符串大小,因为ASCII里面越大的数字反而对应的数字越小,例如'1984' < '1998'  
}

int main() {
    string name;
    int a_num, g_num;
    unordered_map<string, bool> find_alu;
    vector<string> all_guest, alu_list;
    cin >> a_num;
    for (int i = 0; i < a_num; ++i) {
        cin >> name;
        find_alu[name] = true;              // 建立映射map,string -> bool,用来判定一个ID是不是属于校友列表的 
    }
    cin >> g_num;
    for (int i = 0; i < g_num; ++i) {
        cin >> name;
        all_guest.push_back(name);
        if (find_alu[name]) alu_list.push_back(name);  // 如果是校友那么就把ID添加到校友名单中 
    }
    printf("%d\n", alu_list.size());
    if (!alu_list.empty()) {
        sort(alu_list.begin(), alu_list.end(), cmp);    // 用STL的方法来进行排序 
        printf("%s\n", alu_list[0].c_str());
    }
    else {
        sort(all_guest.begin(), all_guest.end(), cmp);
        printf("%s\n", all_guest[0].c_str());
    }
    return 0;
}

这里的代码依旧是和上一篇一样,作者是merely尘埃,我稍微改动了下。

转载于:https://www.cnblogs.com/yejianying/p/cpp_notes_1.html

相关文章:

  • 【第43题】【062题库】2019年OCP认证062考试新题
  • 自我调查 使用输入法
  • linux轻量级监控工具-nmon
  • js基础---变量命名以及运算符
  • JS 原型、原型继承、原型链的理解
  • Linux 双网卡绑定
  • Docker 的基本概念和框架
  • css书写规范
  • Android 8.0允许安装未知来源
  • 蜕变成蝶~Linux设备驱动之中断与定时器
  • 1.9(设计模式)装饰器模式
  • TypeScript Visitor设计模式
  • 构造方法、this关键字的另一种用法
  • 模板 计算1的个数
  • 京北机房 怀来云交换数据中心主机托管
  • python3.6+scrapy+mysql 爬虫实战
  • Create React App 使用
  • Docker下部署自己的LNMP工作环境
  • JavaScript DOM 10 - 滚动
  • JAVA之继承和多态
  • nodejs调试方法
  • python 学习笔记 - Queue Pipes,进程间通讯
  • Spring Cloud中负载均衡器概览
  • spring-boot List转Page
  • use Google search engine
  • 编写符合Python风格的对象
  • 力扣(LeetCode)56
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 少走弯路,给Java 1~5 年程序员的建议
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 携程小程序初体验
  • 在Mac OS X上安装 Ruby运行环境
  • #define
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • $ git push -u origin master 推送到远程库出错
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (C语言)球球大作战
  • (ros//EnvironmentVariables)ros环境变量
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (三)c52学习之旅-点亮LED灯
  • (十八)SpringBoot之发送QQ邮件
  • (四) 虚拟摄像头vivi体验
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • .describe() python_Python-Win32com-Excel
  • .NET 3.0 Framework已经被添加到WindowUpdate
  • .Net CoreRabbitMQ消息存储可靠机制
  • .Net 路由处理厉害了