【codeup】 1928 日期差值

在这里插入图片描述
思路:将较小的日期year1与较大的year2比较,较小的不断加1判断year1和year2是否相等

解题时遇到的小坑:

  1. 输入格式由于是两行一组,没有指定输入个数,如果直接定义int 再cin int类型,不知道怎么判断输入回车结束,所以改成了cin string类型,再转成int类型
  2. 输入格式为两行,但是我不知道如何在cin>>string>>string的情况下判断输入是否为回车,所以改成了每次输入一个string,并用一个step标记目前输入的是year1还是year2

OJ上交代码很奇怪,自己定义的函数交上去AC不了,把函数去了放在main里就能AC了… 昨天晚上因为AC不对一直改还找不到错 简直要发狂了

代码:

#include<bits/stdc++.h> 
using namespace std;
bool judgerunnian(int year)
{
	if(year%4==0 && year%100!=0 || year%400==0)
	return 1;
	else return 0;
}
int main(){
	string yy1,yy2,inf;
	int step=0;
	int y1,y2; 
	int counter=1;//计算相隔的天数 
	int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};	
	int year1,year2;
	int month1,month2;
	int date1,date2;
	while(getline(cin,inf)){
		if(inf.empty())
		break;
		
		if(step%2==0) 
		{
		 yy1=inf;
		}
		if(step%2==1) 
		{
			counter=1;
			yy2=inf;
			y1=atoi(yy1.c_str());
			y2=atoi(yy2.c_str());
			if(y1>y2){
				int temp;
				temp=y1;
				y1=y2;
				y2=temp;
			} 
			year1=y1/10000;
			year2=y2/10000;
			month1=(y1/100)%100;
			month2=(y2/100)%100;
			date1=y1%100;
			date2=y2%100;
			while(year1<year2||month1<month2||date1<date2)
			{
				date1++;
				if(date1>month[month1][judgerunnian(year1)]){
					date1=1;
					month1++;	
				}
				if(month1>12)
				{
					month1=1;
					year1++;
				}
				counter++;	
			}
			cout<<counter<<endl;
		}
		step++;
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值