日期问题
函数模板
int months[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int is_leap(int year)
{
if (year % 4 == 0 && year % 100 || year % 400 == 0)
return 1;
return 0;
}
int get_days(int year, int month)
{
if (month == 2)
return months[month] + is_leap(year);
return months[month];
}
bool check(int date)
{
int year = date / 1000;
int month = date % 1000 / 100;
int days = date % 100;
if (month < 1 || month > 12) return false;
if (day < 1 || day > get_days(year, month)) return false;
return true;
}
输入
int y, m, d;
scanf("%04d%02d%02d", &y, &m, &d);
利用string
合并数字
for (int i = 0; i < 1e8; i ++ )
{
string a = to_string(n);
string b = to_string(i);
int d = stoi(a + b);
if (d % 495 == 0)
{
printf("%d\n", i)
break;
}
}
// 回文处理
for (int i = 1000; i < 9999; i ++ )
{
string a = to_string(i);
string b(a.rbegin(), a.rend());
int date = stoi(a + b);
if (date >= date1 && date <= date2 && check(date))
res ++ ;
}
回文判断
回文数
bool is_Palindrome(int num)
{
int t = num, ans = 0;
while (t) {
ans = ans * 10 + t % 10;
t /= 10;
}
return num == ans;
}
回文串
bool is_Palindrome(char s[])
{
for (int i = 0, j = 7; i < j; i ++, j -- )
if (s[i] != s[j])
return false;
return true;
}
bool is_Palindrome(string s)
{
for(int i = 0; i < s.length() / 2; i ++)
{
if(s[i] != s[s.length() - 1 - i]) return false;
}
return true;
}