C++ 判断闰年程序代码示例
#include <iostream>
#include <string>
using namespace std;
int fun(string str) {
int year = stoi(str);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
}
return 0;
}
int main() {
string str;
cout << '请输入一个年份:';
cin >> str;
if (fun(str) == 1) {
cout << str << '年是闰年' << endl;
} else {
cout << str << '年不是闰年' << endl;
}
return 0;
}
注意事项:
- 需要头文件
<string>和<iostream>。 - 函数
fun(string)中,需要将字符串转换为整数,使用stoi()函数。 - 判断闰年的公式:能被 4 整除但不能被 100 整除,或者能被 400 整除。
原文地址: https://www.cveoy.top/t/topic/oH5a 著作权归作者所有。请勿转载和采集!