题解:P11387 [COCI 2024/2025 #1] 身份证 / Osobna

· · 题解

本人盼了很久,终于到了重打上咕值的机会了,社贡掉了好多。

题目大意

此题题意就不用多说了吧,那我们直接切进思路环节~

解题思路

以下我们成三个字符串分别为 abc,不认识的函数请自行搜索。

首先看前两行输出,与 c 相关,我们直接使用 find 函数和 substr,第一行用 c.find('<'),找出第一个 <,然后输出 c.substr(0 , pos),并在输出前将其除第一个外的所有字符全变为小写。格式我就不多讲了,以下均忽略。

第二行用 c.find('<' , pos + 2),找到继第二个 < 之后的第一个 <,然后输出 c.substr(pos + 2 , pos2 - pos - 2),并在输出前将其除第一个外的所有字符全变为小写。

大写转小写一般采用字符 +32 操作。

此段代码为:

int pos = c.find('<');
string x = c.substr(0 , pos);
for(int i = 1;i < x.size();i++)x[i] += 32;
cout << "Ime: " << x << '\n';
int pos2 = c.find('<' , pos + 2);
x = c.substr(pos + 2 , pos2 - pos - 2);
for(int i = 1;i < x.size();i++)x[i] += 32;
cout << "Prezime: " << x << '\n';

第三行,与 b 有关,日与月,可直接用 substr 输出,分别为 b.substr(4 , 2)b.substr(2 , 2),而年需要判断 b.substr(0 , 2) 的整数形式与 24 的大小,若大于等于 24,则为二十一世纪,反之为二十世纪。

字符串转整数可用 stoi 函数。

此段代码为:

cout << "Datum rodjenja: " << b.substr(4 , 2) << '-' << b.substr(2 , 2) << '-';
if(stoi(b.substr(0 , 2)) <= 24)cout << 20 << b.substr(0 , 2) << '\n';
else cout << 19 << b.substr(0 , 2) << '\n';

最后一行与 a 有关,较易,直接输出 a.substr(15 , 11)

此段代码为:

cout << "OIB: " << a.substr(15 , 11);

这就是整体思路了,下面为完整代码,注释我就不给了。

#include<bits/stdc++.h>
using namespace std;
string a , b , c;
int main(){
    cin >> a >> b >> c;
    int pos = c.find('<');
    string x = c.substr(0 , pos);
    for(int i = 1;i < x.size();i++)x[i] += 32;
    cout << "Ime: " << x << '\n';
    int pos2 = c.find('<' , pos + 2);
    x = c.substr(pos + 2 , pos2 - pos - 2);
    for(int i = 1;i < x.size();i++)x[i] += 32;
    cout << "Prezime: " << x << '\n';
    cout << "Datum rodjenja: " << b.substr(4 , 2) << '-' << b.substr(2 , 2) << '-';
    if(stoi(b.substr(0 , 2)) <= 24)cout << 20 << b.substr(0 , 2) << '\n';
    else cout << 19 << b.substr(0 , 2) << '\n';
    cout << "OIB: " << a.substr(15 , 11);
    return 0;
}