标签:间隔 lse mes ios 1年 == ++ += span
统计日期的天数,主要考虑闰年情况。
思路:
1 #include <iostream> 2 #include<stdio.h> 3 using namespace std; 4 5 int isrun(int year) 6 { 7 if(((year%4==0)&&(year%100!=0))||year%400==0) return 1; 8 else return 0; 9 } 10 int daySum(int year,int month,int day) 11 { 12 int sum=0; 13 int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 14 if(!isrun(year)) months[2]=28; 15 else months[2]=29; 16 for(int i=1;i<=month-1;i++) 17 { 18 sum=sum+months[i]; 19 } 20 sum+=day; //year年的第几天 21 return sum; 22 } 23 int main() 24 { 25 int year,month,day; 26 scanf("%d %d %d",&year,&month,&day); 27 cout<<daySum(year,month,day)<<endl; 28 }
思路:
1 #include <iostream> 2 #include<stdio.h> 3 using namespace std; 4 5 int isrun(int year) 6 { 7 if(((year%4==0)&&(year%100!=0))||year%400==0) return 1; 8 else return 0; 9 } 10 int daySum(int year,int month,int day) 11 { 12 int sum=0; 13 int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 14 if(!isrun(year)) months[2]=28; 15 else months[2]=29; 16 for(int i=1;i<=month-1;i++) 17 { 18 sum=sum+months[i]; 19 } 20 sum+=day; //year年的第几天 21 sum+=(year-1)*365+year/4-year/100+year/400; //加上1到year-1的天数 22 return sum; 23 } 24 int abs(int num) //绝对值函数 25 { 26 if(num<0) return -num; 27 return num; 28 } 29 int main() 30 { 31 int year1,month1,day1,year2,month2,day2; 32 scanf("%d %d %d",&year1,&month1,&day1); 33 scanf("%d %d %d",&year2,&month2,&day2); 34 cout<<abs(daySum(year1,month1,day1)-daySum(year2,month2,day2))<<endl; 35 }
标签:间隔 lse mes ios 1年 == ++ += span
原文地址:https://www.cnblogs.com/lijuanhu321/p/9787968.html