结果是整数,但计算过程要用浮点数计算,不然精度会出问题。
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
int Month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int Monthrun[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
bool judge(int y){
if(y%4!=0) return false;
else if(y%100==0&&y%400!=0) return false;
else return true;
}
int main() {
int n;
int hour,minute,second,day,month,year;
int newh,newmin,news,newd,newm,newy;
long long totalday,totalsecond;
scanf("%d",&n);
while(n--){
scanf("%d:%d:%d %d.%d.%d",&hour,&minute,&second,&day,&month,&year);
totalday=day-1;
for(int i=2000;i<year;i++){
if(judge(i)==true) totalday+=366;
else totalday+=365;
}
for(int i=1;i<month;i++){
if(judge(year)==true) totalday+=Monthrun[i];
else totalday+=Month[i];
}
double t=(totalday*86400+hour*3600+minute*60+second)*100000/86400;
totalsecond=t;
newy=totalsecond/(int)(pow(10,8));
newm=(totalsecond%(int)(pow(10,8)))/(int)(pow(10,7))+1;
newd=(totalsecond%(int)(pow(10,7)))/(int)(pow(10,5))+1;
newh=(totalsecond%(int)(pow(10,5)))/(int)(pow(10,4));
newmin=(totalsecond%(int)(pow(10,4)))/(int)(pow(10,2));
news=(totalsecond%(int)(pow(10,2)));
printf("%d:%d:%d %d.%d.%d\n",newh,newmin,news,newd,newm,newy);
}
return 0;
}