本博客仅供个人记录学习上遇到的问题和复盘
这道题做了好久,调试了好久。其实也就是将日期转换成二进制字符串,然后判断二进制字符串是否是回文串。其中有一些坑,比如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;
}