计算[1900, year]之间的闰年数

来源:互联网 发布:马鞍山网络招聘网 编辑:程序博客网 时间:2024/05/01 22:40
#include <iostream>using namespace std;/* * [1900, year] 之间闰年的数目 * created by dyc*/int num_of_leapyear(int year){if (year <= 1900){return 0;}int h = year / 100;int t = year % 100;int count = 0;for (int i = 19; i <= h; i++){if (i%4 == 0)count++;}count += (h - 19) * 24;count += t/4;    return count;}int main(){int year;while ( cin >> year){cout << num_of_leapyear(year) << "\n";}return 0;}

做过一些测试,结果正确。希望大家能够帮助提供用例验证
原创粉丝点击