PAT B1111 对称日

2025-04-11  本文已影响0人  梅友泥撑

自己写的和人家写的,差距还是挺大的,得学啊...

#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
bool judge(string str) {
    string temp = str;
    reverse(temp.begin(), temp.end());
    if (temp == str)
        return true;
    return false;
}
int main() {
    map<string, string> month = { {"Jan","01"},{"Feb","02"},{"Mar","03"},{"Apr","04"},{"May","05"},{"Jun","06"},{"Jul","07"},{"Aug","08"},{"Sep","09"},{"Oct","10"},{"Nov","11"},{"Dec","12"} };
    string date_mon;
    int day, year, n;
    cin >> n;
    while (n--) {
        cin >> date_mon;
        scanf("%d,%d", &day, &year);
        string yearstr = to_string(year);
        yearstr.insert(yearstr.begin(), 4 - yearstr.length(), '0');
        string daystr = to_string(day);
        daystr.insert(daystr.begin(), 2 - daystr.length(), '0');
        string ans = yearstr + month[date_mon] + daystr;
        if (judge(ans)) {
            cout << "Y " << ans << endl;
        }
        else {
            cout << "N " << ans << endl;
        }
    }
    return 0;
}
#include <iostream>
#include <map>
using namespace std;
int t;
string M, D, Y, final, symmetry;
map<string,string> A = {{"Jan", "01"}, {"Feb", "02"}, {"Mar", "03"}, {"Apr", "04"}, {"May", "05"}, {"Jun", "06"}, {"Jul", "07"}, {"Aug", "08"}, {"Sep", "09"}, {"Oct", "10"}, {"Nov", "11"}, {"Dec","12"}};
int main(){
    for (cin >> t; t; t--) {
        cin >> M >> D >> Y;
        D.erase(D.end() - 1);
        M = A[M];
        if(D.size() == 1) D = "0" + D;
        while(Y.size() < 4) Y = "0" + Y;
        final = Y + M + D;
        symmetry = "Y";
        for (int i = 0; i < 4; i++) {
            if(final[i] != final[8 - i - 1]) {
                symmetry = "N";
                break;
            }
        }
        cout << symmetry << ' ' << final << '\n';
    }
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读