程序员算法趣题 Q07 日期的二进制转换 C++实现

这篇博客记录了作者解决一个算法问题的过程,即如何将日期转换为二进制字符串,并检查该字符串是否为回文。在实现中,遇到了如重置流状态和月份前导零填充等难点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本博客仅供个人记录学习上遇到的问题和复盘

这道题做了好久,调试了好久。其实也就是将日期转换成二进制字符串,然后判断二进制字符串是否是回文串。其中有一些坑,比如stream的重置流的标志状态(注意不是清空流),还有日期月份小于10得加多0等。

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cmath>

using namespace std;

//注意stream的重置标记位,和月份和日期少于10的处理


// bool jude(int key)//判断回文
// {
//     int num = key;
//     int key1 = 0;
//     vector<int> v;
//     while(num != 0)
//     {
//         v.push_back(num % 10);
//         num /= 10;        
//     }
//     int time = 0;//标记位数
//     for (vector<int>::iterator i = v.end() - 1; i != v.begin() - 1;--i)
//     {
//         key1 += *i * pow(10, time++);
//     }
//     // cout << key1 << endl;
//     return key == key1;
// }



string reversed(string target)
{
    string target2 = "";
    for (int i = target.size() - 1; i >= 0; --i)//有个\0
    {
        target2 += target[i];
    }
    return target2;
}

// int tr10(string target)
// {

// }
string tr2(int target)
{
    vector<int> v;
    while (target != 0)
    {
        v.push_back(target % 2);
        target /= 2;
    }
    string target2 = "";

    for (vector<int>::iterator i = v.begin(); i != v.end(); ++i)
    {
        int temp = *i;
        target2 += to_string(temp);
    }
    return reversed(target2);
}
bool judgeYears(int target)
{
    if (target % 400 == 0 || (target % 4 == 0 && target % 100 != 0))
    {
        return true;
    }
    return false;
}

bool judegePaString(string target) //判断回文串
{
    int tLength = target.size() - 1;
    bool judge = true;
    for (auto i : target)
    {
        if (i != target[tLength])
        {
            judge = false;
        }
        tLength--;
    }
    return judge;
}
void solve()
{
    vector<int> months1 = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    vector<int> months2 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    stringstream ss;
    int target = 0;
    string s = "";
    string temp = "";
    for (int year = 1966; year <= 2020; ++year)
    {
        for (int month = 1; month <= 12; ++month)
        {
            if (judgeYears(year))
            {
                for (int day = 1; day <= months2[month - 1]; ++day)
                {
                    if (month < 10)
                    {
                        s = to_string(year) + "0" + to_string(month) + to_string(day);
                    }
                    else
                    {
                        s = to_string(year) + to_string(month) + to_string(day);
                    }

                    if (day < 10)
                    {
                        s = s.insert(s.size() - 1, "0");
                    }

                     //cout << s << endl;
                    ss << s;
                    ss >> target;
                     ss.clear();
                    temp = tr2(target);
                    if (judegePaString(temp))
                    {
                        cout << s << endl;
                    }
                }
            }
            else
            {
                for (int day = 1; day <= months1[month - 1]; ++day)
                {
                    if (month < 10)
                    {
                        s = to_string(year) + "0" + to_string(month) + to_string(day);
                    }
                    else
                    {
                        s = to_string(year) + to_string(month) + to_string(day);
                    }
                    if (day < 10)
                    {
                        s = s.insert(s.size() - 1, "0");
                    }
                   // cout << s << endl;
                    ss << s;
                    ss >> target;
                    temp = tr2(target);
                    ss.clear();
                    if (judegePaString(temp))
                    {
                        cout << s << endl;
                    }
                }
            }
        }

    }
}
// void solve(int key)
// {
//     vector<int> months1 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//     vector<int> months2 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//     string s = "";
//     stringstream ss;
//     string target2 = "";
//     int target = 0;
//     string maxKey = reversed(tr2(key));

//     for (int year = 1964; year <= 2020; ++year)
//     {
//         for (int month = 1; month <= 12; ++month)
//         {
//             if(judgeYears(year))
//             {
//                 for (int day = 1; day <= months2[month]; ++day)
//                 {
//                     s = to_string(year) + to_string(month) + to_string(day);

//                     ss << s;
//                     ss >> target;
//                     target2 = tr2(target);
//                     if(target2 == maxKey)
//                     {
//                         cout << s << endl;
//                     }
//                 }
//             }
//             else
//             {
//                 for (int day = 1; day <= months1[month]; ++day)
//                 {
//                     s = to_string(year) + to_string(month) + to_string(day);

//                     ss << s;
//                     ss >> target;
//                     target2 = tr2(target);
//                     if(target2 == maxKey)
//                     {
//                         cout << s << endl;
//                     }
//                 }
//             }
//         }

//     }
// }

int main()
{
    // cout << tr2(9) << endl;
    // string s = to_string(1 + 2) + "a";
    // s += to_string(2);
    // cout << s << endl;

    //cout << reversed("123") << endl;

    // stringstream ss;
    // ss << "123";
    // int a = 0;
    // ss >> a;
    // cout << a << endl;

    //solve(19660713);

    // cout << reversed(tr2(19660713)) << endl;
    // cout << tr2(19660905) << endl;
    // cout << tr2(19770217) << endl;
    // string a = "123";
    // cout << a.size() << endl;

    // cout << judegePaString("121321") << endl;

    //cout << tr2(8) << endl;
    //solve();


    // string temp = "";
    // int target = 19660905;
    // temp = tr2(target);
    // cout << temp << endl;
    // if (judegePaString(temp))
    // {
    //     cout << 1 << endl;
    // }

    // cout << judegePaString("12321\0") << endl;
    solve();

    // string s1 = "123\0";
    // cout << judegePaString("12321\0") << endl;
    // cout << s1 << endl;
    // cout << s1.size() << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒜蓉蒸大虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值