計算兩個日期之間所間隔的天數。交作業的時候有兩組資料的測驗結果錯了
一組正確的是16.輸出了15
另外一組正確的是13754,輸出13755
求大佬幫忙看看,謝謝謝謝!!
#include<iostream>
using namespace std;
int main()
{
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int year1,year2,mon1,mon2,day1,day2,i,m1=0,n1=0,n2=0,sum=0,t1=0,t2=0,t;
cin>>year1>>mon1>>day1;
cin>>year2>>mon2>>day2;
for(i=0;i<12;i++)
{
if(mon1==i+1)
m1=mon[i]-day1;
}
for(i=mon1;i<12;i++)
{
n1+=mon[i];
}
for(i=0;i<mon2-1;i++)
{
n2+=mon[i];
}
if((year1 % 4 ==0 || year1 % 400 ==0) && (year1 % 100 !=0))
{if(mon1<=2)
if(day1!=29){t1++;}}
if((year2 % 4 ==0 || year2 % 400 ==0) && (year2 % 100 !=0))
{if(mon2>2)t2++;}
for(i=year1+1,t=0;i<year2;i++)
{
if((i % 4 ==0 || i % 400 ==0) && (i % 100 !=0)){t++;}
}
sum=m1+n1+n2+(year2-year1-1)*365+day2+t+t1+t2;
cout<<sum;
return 0;
}
uj5u.com熱心網友回復:
判斷閏年的錯了吧,應該是year1 % 4 ==0 && year1 % 100 !=0 || year1 % 400 ==0uj5u.com熱心網友回復:
僅供參考:#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
COleDateTime t;
COleDateTimeSpan ts;
CString s,fmt;
int nYear;
int nMonth;
int nDay;
int nHour;
int nMin;
int nSec;
int lDays;
int nHours;
int nMins;
int nSecs;
int i,N;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
printf("Fatal Error: MFC initialization failed\n");
return 1;
}
if (argc<13) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 2;
}
if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";
else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";
else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";
else {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 3;
}
nYear =atoi(argv[ 1]);
nMonth=atoi(argv[ 2]);
nDay =atoi(argv[ 3]);
nHour =atoi(argv[ 4]);
nMin =atoi(argv[ 5]);
nSec =atoi(argv[ 6]);
lDays =atoi(argv[ 7]);
nHours=atoi(argv[ 8]);
nMins =atoi(argv[ 9]);
nSecs =atoi(argv[10]);
N =atoi(argv[11]);
if (N<=0) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 4;
}
t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);
ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );
for (i=1;i<=N;i++) {
s=t.Format(fmt);
printf("%08d %s\n",i,s);
t=t+ts;
}
return 0;
}
uj5u.com熱心網友回復:
#include <stdio.h>//獲取公歷年的天數
int year_alldays(int year)
{
if((year%4==0 && year%100!=0) || year%400==0) return 366; else return 365;
}
//獲取公歷年初至某整月的天數
int year_sumday(int year,int month)
{
int sum=0;
int rui[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int ping[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int ruiflag=0;
if((year%4==0 &&year%100!=0) || year%400==0) ruiflag=1;
for(int index=0;index<month-1;index++)
{
if(ruiflag==1) sum+=rui[index];else sum+=ping[index];
}
return sum;
}
//獲取從公歷1800年1月25日至當前日期的總天數
int get_g_alldays(int year,int month,int day)
{
int i=1800,days=-24;
while(i<year)
{
days+=year_alldays(i);
i++;
}
int days2=year_sumday(year,month);
return days+days2+day;
}
int main(void)
{
int year1,year2,month1,month2,day1,day2;
printf("請輸入起始日期 輸入格式yyyy-mm-dd:\n");
scanf("%d-%d-%d",&year1,&month1,&day1);
printf("請輸入終止日期 輸入格式yyyy-mm-dd:\n");
scanf("%d-%d-%d",&year2,&month2,&day2);
int days1=get_g_alldays(year1,month1,day1);
int days2=get_g_alldays(year2,month2,day2);
int sout=0;
if(days1>=days2) sout=days1-days2;else sout=days2-days1;
printf("%4d年%2d月%2d日 到 %4d年%2d月%2d日 間隔 %8d 天.\n",year1,month1,day1,year2,month2,day2,sout);
return 0;
}
uj5u.com熱心網友回復:
判斷閏年的錯了吧,應該是year1%4 == 0 && year1%100!= 0 || 年1%400 == 0uj5u.com熱心網友回復:
可參https://ask.csdn.net/questions/1063222#answer_1261149轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84727.html
標籤:C++ 語言
