PAT(乙级) 4045人口普查(C++)

在这里插入图片描述
在这里插入图片描述

✨ 只需要判断 年 是否大于200年,如果多一天或者多一个月都是大于200,(多的天数和月数的总和是不可能大于一年的),所以只需要加减来判断最后的总和

✨ 字符的拆分,借鉴的博客:戳戳戳~
里面有很多自带的函数,尤其是操作的函数的参数

✨ 输入时,在读取完人名之后会读取到一个空格,当时去搜索了做法:getcin(但是没用起来…最后还是用数组下标操作起来的)

✨ 还要注意vector用法

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
using namespace std;

vector<string> split(const string &s,const string & dim){
    vector<string> vec;
    if(s == "") return vec;
    char *c = new char[s.length()+1];
    strcpy(c,s.c_str());//将string转换为char

    char *d = new char[dim.length()+1];
    strcpy(d,dim.c_str());

    char *p = strtok(c,d);
    while(p){
        string s1 = p;
        vec.push_back(s1);
        p = strtok(NULL, d);
    }
    return vec;


}


int main(){
    int num;
    cin>>num;
    string s1[2*num];
    for(int i=0;i<num;i++){
       cin>>s1[2*i]>>s1[2*i+1];
       // cout<<s1[i]<<endl;
    }
    int year =2014;
    int mon = 9;
    int day = 6;

    int count1 = 0;
    string dim = "/";
    int maxage =0;
    int minage = 1000;
    string maxname = "";
    string minname = "";
    for(int i=0;i<num;i++){
        string s = s1[2*i+1];
        vector<string> temp = split(s,dim);
        //cout<<temp[0]<<"   "<<temp[1]<<"   "<<temp[2]<<endl;
       // int m =(temp[0][0]-'0')*1000+(temp[0][1]-'0')*100+(temp[0][2]-'0')*10+(temp[0][3]-'0');
        //cout<<m<<endl;
        if(temp[0][0]-'0' >year||(temp[0][0]-'0'==year&&temp[1][0]-'0'>mon)||(temp[0][0]-'0'==year&&temp[1][0]-'0'==mon&&temp[2][0]-'0'>day)){

        }else {

            int y = year-((temp[0][0]-'0')*1000+(temp[0][1]-'0')*100+(temp[0][2]-'0')*10+(temp[0][3]-'0'));
            int m = mon-((temp[1][0]-'0')*10+(temp[1][1]-'0'));
            int d = day - ((temp[2][0]-'0')*10+(temp[2][1]-'0'));

            if(y+m+d<=200&y+m+d>=0){
                    if(y+m+d>maxage){
                        maxage = y+m+d;
                        maxname = s1[2*i];
                    }
                    if(y+m+d<minage){
                        minage = y+m+d;
                        minname = s1[2*i];
                    }
                count1++;
            }
        }

    }

    cout<<count1<<" "<<maxname<<" "<<minname<<endl;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值