题目描述
定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
输入
年月日
输出
当年第几天
#include<iostream>
using namespace std;
struct time
{
int year;
int month;
int date;
};
struct time num;
int main()
{
cin >> num.year;
cin >> num.month;
cin >> num.date;
int sum=0;
int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if ((num.year % 4 == 0 && num.year % 100 != 0) || num.year % 400 == 0)//闰年情况
a[1] = 29;
else
a[1] = 28;
for (int i = 0; i < num.month-1; i++)
sum += a[i];
sum = sum + num.date;
cout << sum;
return 0;
}
注意闰年的判断:
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
运行结果: