第5章练习题--2--特殊日历计算--2967

#include <stdio.h>
#include <stdlib.h>

int LeapYear(int y)
{
    if ( y%4!=0 || (y%100==0 && y%400!=0) ) return 0;
    else return 1;
}

int main(int argc, char *argv[])
{
  int n, i , l;
  int month[2][13] = {0,31,28,31,30,31,30,31,31,30,31,30,31,
                      0,31,29,31,30,31,30,31,31,30,31,30,31};
  int h, min, s, d, mon, y,  mh, mmin, ms, md, mmon, my;
  long days;
  long long int seconds;
  
  scanf("%d",&n);
  while(n--)
  {
      scanf("%d:%d:%d %d.%d.%d",&h,&min,&s,&d,&mon,&y);
      days = 0 ;
      seconds =  0;
      l = LeapYear(y);
      days += (long)(d - 1) ; 
	  for( i = 2000 ; i<y ; i++ )
		  if(LeapYear(i)==0) days += 365 ;
		  else days += 366 ;
      for( i = 0 ; i<=(mon-1) ; i++ )
          days += (long)month[l][i] ;
      seconds =(long long)days*86400 ;
	  seconds += h*3600 + min*60 + s;
      seconds = seconds*1000 ;
      seconds = seconds/864 ;
      ms = seconds%100 ;
      seconds = seconds/100 ;
      mmin = seconds%100 ;
      seconds = seconds/100;
      mh = seconds%10 ;
      seconds = seconds/10 ;
      md = seconds%100 ; 
      md++ ;
      seconds = seconds/100;
      mmon = seconds%10;
      mmon++ ;
      seconds = seconds/10;
      my = seconds;
      printf("%d:%d:%d %d.%d.%d\n",mh,mmin,ms,md,mmon,my);
  }
  
  system("PAUSE");	
  return 0;
}

这道题比较绕,只要搞清楚逻辑关系就好,但是要注意一个地方:数据类型long long int 和_int64的使用

用_int64 的seconds提交,会出现CE,可能OJ不支持这个类型吧,于是改成使用long long int ,就AC了

关于long long int和_int64,见博文

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值