C++ 函数判断闰年 (fun(string))
以下是用 C++ 编写的函数 fun(string),用于判断一个字符串表示的年份是否为闰年:
int fun(string year) {
int y = stoi(year); // 将字符串转换为整数
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
return 1; // 是闰年,返回 1
} else {
return 0; // 不是闰年,返回 0
}
}
函数中首先将字符串转换为整数,然后使用了闰年的判定规则:能被 4 整除但不能被 100 整除,或者能被 400 整除的年份为闰年。如果符合条件,则返回 1,否则返回 0。
原文地址: https://www.cveoy.top/t/topic/oH4X 著作权归作者所有。请勿转载和采集!