百炼2967:特殊日历计算

开始的时候,没有理解题意,没有想到两种日历的方式同一天的时间是一样的,分析如下

代码为:

#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;
}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值