L1-075 强迫症 (10 分)

摘要
Title: L1-075 强迫症 (10 分)
Tag: string
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

L1-075 强迫症 (10 分)

  • 题意

    小强在统计一个小区里居民的出生年月,但是发现大家填写的生日格式不统一,例如有的人写 199808,有的人只写 9808。有强迫症的小强请你写个程序,把所有人的出生年月都整理成 年年年年-月月 格式。对于那些只写了年份后两位的信息,我们默认小于 22 都是 20 开头的,其他都是 19 开头的。

  • 思路

    补充C++ string的相关函数

    • stoll 将string转化为long long
    • to_string() 将任意类型转化为string
    • strstr(s1, s2) kmp的复杂度,找到s2在s1中的第一个下标
    • string.c_str() 可以理解为string转化为char数组输出,这样就可以%s输出了
  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    /*
    * @Author: NEFU AB-IN
    * @Date: 2022-04-22 16:45:11
    * @FilePath: \ACM\GPLT\L1-075.CPP
    * @LastEditTime: 2022-04-22 16:56:30
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define MP make_pair
    #define SZ(X) ((int)(X).size())
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(0); \
    cout.tie(0);
    #define DEBUG(X) cout << #X << ": " << X << endl;
    typedef pair<int, int> PII;
    const int INF = 0x3f3f3f3f;

    signed main()
    {
    IOS;
    string s, year, month;
    cin >> s;
    if (SZ(s) == 6)
    {
    year = s.substr(0, 4);
    month = s.substr(4, 2);
    printf("%s-%s", year.c_str(), month.c_str());
    }
    else
    {
    year = s.substr(0, 2);
    month = s.substr(2, 2);
    int y = stoll(year);
    if (y < 22)
    printf("20%s-%s", year.c_str(), month.c_str());
    else
    printf("19%s-%s", year.c_str(), month.c_str());
    }

    return 0;
    }
使用搜索:谷歌必应百度