开始的时候,没有理解题意,没有想到两种日历的方式同一天的时间是一样的,分析如下
代码为:
#include <stdio.h>
#include <stdlib.h>
//判断年是不是闰年
int isRunYear(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else
return 0;
}
int main()
{
int hour,minute,second,day,month,year;
int mhour,mmin,msec,mday,mmonth,myear;
int dayofmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30};
int n;
scanf("%d",&n);
while(n--){
scanf("%d:%d:%d %d.%d.%d",&hour,&minute,&second,&day,&month,&year);
if(year<2000||year>50000)
return 1;
int s=0;
int days=0;
days=days+(year-2000)*365;
if(isRunYear(year)==1)
dayofmonth[2]=29;
while(year>=2000)
{
int x=year-1;
if(isRunYear(x)==1)
days+=1;
year--;
}
int i;
for(i=0;i<month;i++){
days+=dayofmonth[i];
}
days+=day-1;
myear=days/1000;
days=days%1000;
mmonth=days/100+1;
mday=days%100+1;
s=(hour*3600+minute*60+second)*125/108;
mhour=s/10000;
s-=mhour*10000;
mmin=s/100;
msec=s-mmin*100;
printf("%d:%d:%d %d.%d.%d\n",mhour,mmin,msec,mday,mmonth,myear);
}
return 0;
}
不知道为什么,提交后是wrong answer,今天做了两道题,不能把自己弄得太吃力,就这样吧,以后有机会再弄
============================================================================================================
第二天早上:
是自己月份处理出现了问题。。所以遇到wrong answer的时候,耐心一些,慢慢解决问题
下面附上新的code
错的原因见注释
#include <stdio.h>
#include <stdlib.h>
//判断年是不是闰年
int isRunYear(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else
return 0;
}
int main()
{
int hour,minute,second,day,month,year;
int mhour,mmin,msec,mday,mmonth,myear;
//通过二维数组的方式存储月的情况,主要是闰年的2月和平年的2月
int dayofmonth[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30},{0,31,29,31,30,31,30,31,31,30,31,30}};
int n;
scanf("%d",&n);
while(n--){
scanf("%d:%d:%d %d.%d.%d",&hour,&minute,&second,&day,&month,&year);
if(year<2000||year>50000)
return 1;
int s=0;
int days=0;
days=days+(year-2000)*365;
// if(isRunYear(year)==1)这种方法是错误的,如果输入的这一年是闰年,则将月改了后,月都变成了29,没有了28
//dayofmonth[2]=29;
int yy;
yy=year;
while(year>=2000)
{
int x=year-1;
if(isRunYear(x)==1)
days+=1;
year--;
}
int i;
for(i=0;i<month;i++){
days+=dayofmonth[isRunYear(yy)][i];
}
days+=day-1;
myear=days/1000;
days=days%1000;
mmonth=days/100+1;
mday=days%100+1;
s=(hour*3600+minute*60+second)*125/108;
mhour=s/10000;
s-=mhour*10000;
mmin=s/100;
msec=s-mmin*100;
printf("%d:%d:%d %d.%d.%d\n",mhour,mmin,msec,mday,mmonth,myear);
}
return 0;
}