题解 | 今年的第几天?

今年的第几天?

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

#include <stdio.h>  
void TotalDay(int year,int month,int day){
	int dayofMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int isLeap=(year%400==0)||(year%4==0&&year%100!=0);
	if(isLeap){
		dayofMonth[2]=29;
	} else{
		dayofMonth[2]=28;
	}
	int count=0;
	if(month==1){
		count=day;
		printf("%d",count);
	}
	if(month>1){
		for(int i=1;i<month;i++){
			count=count+dayofMonth[i];
		}
		count=count+day;
		printf("%d",count);
	} 
}
int main(){
	int year,month,day;
	while(scanf("%d %d %d",&year,&month,&day)!=EOF){
		if(month < 1 || month > 12) {
            printf("月份输入错误,请输入1-12之间的数!\n");
            continue;
        }
		TotalDay(year,month,day);
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务