题解 | #今年的第几天?#

今年的第几天?

https://www.nowcoder.com/practice/ae7e58fe24b14d1386e13e7d70eaf04d

#include <bits/stdc++.h>

using namespace std;

const int months[]={
	0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

int is_leap(int year){
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1;
	return 0;
}
// 第year年第month月的天数 
int get_days(int year, int month){
	int res = 0;
	res += months[month];
	if (month == 2){
		res += is_leap(year);
	}
	return res;
}
int main(){
	
	int year, month, day;
	
	while (cin >> year >> month >> day){
		int res = 0;
		for (int i=1; i<month; i++){
			res += get_days(year, i);
		}
		res += day;
		printf("%d\n", res);
	}
	
	return 0;
}

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
05-14 18:44
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务