[C++ written test questions] Calculating a certain day is the day of the year

Programming questions
Enter the year, month, and day, and calculate the day of the year that this day is. Any year that meets the following two conditions is a leap year, and there will be 29 days in February in a leap year:

1. The year is a multiple of 4 but not a multiple of 100.

2. The year is a multiple of 40

Enter a description:

Enter the format of the date: Year, Month, Day contains three integers: year (1<=Year<=3000), month (1<=Month<=12), day (1<=Day<=31)

# include<iostream>
# include<bits/stdc++.h>
using namespace std;



int main() {
    
    
	int year, month, day;
	int month_days[12] = {
    
     31, 28, 31, 30, 31, 30, 31, 31, 30, 30, 31 };
	cout << "请输入年,月,日:" << endl;
	scanf("%d,%d,%d", &year, &month, &day);
	if ((year % 4 == 0 && year % 100 != 0) || (year % 40 == 0)) {
    
    
		month_days[1] = 29;
	}
	int result = 0;
	for (int i = 0; i < month-1; i++) {
    
    //累计计算month月之前的总天数
		result += month_days[i];
	}
	result += day;//加上month那个月的天数
	cout << result << endl;
	return 0;

}

おすすめ

転載: blog.csdn.net/qq_43050258/article/details/130231275