蓝桥杯相关前置内容
c++int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
最小公倍数=两数乘积/最大公约数
c++int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b); //最大公约数
}
int main()
{
int x = a * b / gcd(a, b); //最小公倍数
}
c++int main
{
int q[100];
int cnt = -1;
while (x)
{
q[++cnt] = x % 10;
x /= 10;
}
}
c++#include <iostream>
#include <cstdio>
using namespace std;
struct date
{
int year, month, day;
void print()
{
printf("%04d-%02d-%02d\n", year, month, day);
}
};
int DayNum[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //12个月的日期数目
bool correct(date& x)
{
if (x.month != 2 && 0 < x.day && x.day < DayNum[x.month]) return true;
else if (x.month == 2) //二月单独考虑
{
int extra = x.year % 400 == 0 || (x.year % 4 == 0 && x.year % 100 != 0); //闰年判断
if (0 < x.day && x.day < DayNum[x.month] + extra) return true;
}
return false;
}
date construct(int x) //x是日期,如20220213
{
date m = { x / 10000, x % 10000 / 100, x % 100 }; //C++11构造方法,注意参数顺序必须与结构体的定义顺序一致
bool y = correct(m); //判断日期是否有效
if (y) {
m.print();
return m;
}
else {
printf("Construction failed\n");
}
}
bool compare(date x, date y)
{
if (x.year != y.year) return x.year < y.year;
else
{
if (x.month != y.month) return x.month < y.month;
else return x.day < y.day;
}
}
int main()
{
int x;
scanf("%d", &x);
auto m = construct(x);
int y = 20230524;
auto n = construct(y);
bool flag = compare(m,n);
cout << flag << endl;
}
本文作者:Travis
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 Travis 许可协议。转载请注明出处!