2019-01-25 16:38

# codeup 1928: 日期差值，答案错误，我看书上也差不多是这样写，为什么就是通不过

20130101
20130105

5

``````#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int isLeap(int year);

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 main(){

int temp,sel,date1,date2,days = 1;
int year1,year2,months1,months2,day1,day2;
while(cin >> date1 && cin >> date2){
if(date1 > date2){
temp = date1;
date1 = date2;
date2 = temp;
}
year1 = date1 / 10000;
year2 = date2 / 10000;
months1 = (date1 / 100) % 100;
months2 = (date2 / 100) % 100;
day1 = date1 % 100;
day2 = date2 % 100;

sel = isLeap(year1);
while(year1 < year2 || months1 < months2 || day1 < day2){
day1++;
days++;
if(day1 == month[months1][sel] + 1 ){
day1 = 1;
months1++;
}
if(months1 == 13){
months1 = 1;
year1++;
sel = isLeap(year1);
}
}
cout << days << endl;
}
return 0;
}

int isLeap(int year){
return (year % 4 == 0 )&&(year % 100 ！= 0)||(year % 400 == 0);
}
``````
• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• hpu刘 2019-01-25 16:56
已采纳

闰年的判断方法不对，正确判断是否为闰年的表达式应为：(year % 4 == 0 )&&(year % 100 != 0)||(year % 400 == 0)

点赞 打赏 评论
• 任桐秀 2019-01-28 19:02

抱歉，之前回答时没有看到回复，但由于无法删除评论所以只能这样了。

点赞 打赏 评论