天数——日期问题

随意输入一个年月日,计算那天是那年的第几天。

输入格式

输入三个整数,分别代表 年 月 日,用空格隔开。

输出格式

输出一个整数,表示输入的日期是当年第几天。

样例输入
2022 10 15
样例输出
288
样例输入
2022 6 8
样例输出

159

代码:

#include<iostream>
using namespace std;

int daynum(int year,int moth){
	int day;
	if(moth==1||moth==3||moth==5||moth==7||moth==8||moth==10||moth==12){
		day=31;
	}else if(moth==2){
		if(year%4==0&&year%100!=0||year%400==0){  //闰年
			day=29;
		}else{
			day=28;
		}
	}else{  //4,6,9,11月
		day=30;
	}
	return day;
}

int main(){
	int year,moth,day;
	
	while(cin>>year>>moth>>day){
		int sum=0;
		for(int i=1;i<moth;i++){
			sum+=daynum(year,i);
		}
		sum+=day;
		cout<<sum<<endl;
	}
	return 0;
}

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值