FOJ 1324 Time

本文分享了一种计算两个日期间的时间差方法,通过逐步拆解时间单位(年、月、日、小时、分钟、秒),实现了从年到秒的精确转换。文章通过两个不同长度变量名的代码示例对比,强调了简洁变量名的重要性,并总结了注释中使用英文字符避免编译错误的经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2010-11-07 22:15:27

【题目地址】http://acm.fzu.edu.cn/problem.php?pid=1324

收获:1.变量名名称太长,不易于观看~~~~

           2.懂得注释的一些细节~~~

                         【fatal error C1071: unexpected end of file found in comment  查了下:注释里有中文等字符的时候,   在单行注释(// )最后加个空格,   多行注释  (  */ )前加一个空格   ...当然最好的是注释都用英文。

这道题费了我好多时间,纠结了好久,可能是因为用的变量名太长了,所以在哪里出错了,至今找不出来,所以决定重新做题,把变量名化为比较短的,最终终于AC了!!!!+_+

AC代码:

 


 

#include<stdio.h>
#include<string.h>
int f(int y)
{
    if(y%400==0||y%100!=0&&y%4==0) return 1;
    else return 0;
}
int main()
{
    struct
    {
        int y,m,d,h,mi,s;
    }st,nd;
    int i,r,n,flag,leap,ndays[]=

{0,31,28,31,30,31,30,31,31,30,31,30,31};
    char str0[7],str[][7]=

{"year","month","day","hour","minute","second"};
    while(scanf("%d%d%d%d%d%d",&st.y,&st.m,&st.d,&st.h,&st.mi,&st.s)!

=EOF)
    {
        scanf("%d%d%d%d%d

%d",&nd.y,&nd.m,&nd.d,&nd.h,&nd.mi,&nd.s);
        scanf("%d%s",&r,str0);
        for(i=0;i<6;i++) if(strcmp(str[i],str0)==0) { flag=i;

break;}
        n=nd.y-st.y;
        if(flag==0&&n!=0&&(st.m!=1||st.d!=1||st.h!=0||st.mi!=0||

st.s!=0)) n--;
        if(flag==1)
        {
            n=n*12+(nd.m-st.m);
            if(n!=0&&(st.d!=1||st.h!=0||st.mi!=0||st.s!=0))

n--;
        }
        if(flag>1)
        {
            if(n!=0) n=(n-1)*365;
            else {leap=f(st.y); n=(n-1)*(365+leap);}
            for(i=st.y+1;i<nd.y;i++) {leap=f(i); n+=leap;}// 

求中间年份的天数
            for(i=st.m;i<13;i++) n+=ndays[i];
            n-=st.d;
            if(st.m<3) n+=f(st.y);//   求头年的天数
            for(i=1;i<nd.m;i++) n+=ndays[i];
            n+=nd.d;
            if(nd.m>=3) n+=f(nd.y); //  求尾年的天数
            if(flag==2&&n!=0&&(st.h!=0||st.mi!=0||st.s!=0))

n--;
        }
        if(flag>2)
        {
            n=n*24+(nd.h-st.h);
            if(flag==3&&n!=0&&(st.mi!=0||st.s!=0)) n--;
        }
        if(flag>3)
        {
            n=n*60+(nd.mi-st.mi);
            if(flag==4&&n!=0&&st.s!=0) n--;
        }
        if(flag>4) n=n*60+(nd.s-st.s);
        printf("%d/n",n/r);
    }
    return 0;
}


 


传说中的WA代码:~~~~(>_<)~~~~

 


 

#include<stdio.h>
#include<string.h>
typedef struct
{ int year,month,day,hour,minute,second;
} TIME;
int main()
{
    TIME time[2],*p1,*p2;
    int i,m,n,flag,leap,ndays[]={31,28,31,30,31,30,31,31,30,31,30,31};
    char str[][7]={"year","month","day","hour","minute","second"},str0[7];
    p1=time; p2=p1+1;
    while(scanf("%d%d%d%d%d%d",
        &p1->year,&p1->month,&p1->day,&p1->hour,&p1->minute,&p1-

>second)!=EOF)
    {
        scanf("%d%d%d%d%d%d",&p2->year,&p2->month,&p2->day,&p2-

>hour,&p2->minute,&p2->second);
        scanf("%d%s",&m,str0);
        getchar(); getchar();
        for(i=0;i<6;i++) if(strcmp(str[i],str0)==0)

{flag=i;break;}/*year=0;month=1;day=2;hour=3;min=4;sec=5.*/
        n=p2->year-p1->year;
        if(flag==0&&n!=0)if(!(p1->month==1&&p1->day==1&&p1-

>hour==0
            &&p1->minute==0&&p1->second==0)) n--;
        if(flag==1)
        {n=n*12+(p2->month-p1->month); if(n!=0&&!(p1->day==1&&p1-

>hour==0
            &&p1->minute==0&&p1->second==0)) n--;
        }
        if(flag>1)
        {
            if(n!=0) n=365*(n-1);
            else {i=p1->year; leap=( i%400==0)||(i%100!=0)&&

(i%4==0); n=366*(n-1);}/*中间年份 年->天 */
            for(i=p1->year+1;i<p2->year;i++) {leap=(i

%400==0)||(i%100!=0)&&(i%4==0); n+=leap;}
            for(i=p1->month;i<13;i++)  n+=ndays[i-1];/*头年 月

->天 */
            n+=-p1->day;
            if(p1->month<3)
            {i=p1->year; leap=( i%400==0)||(i%100!=0)&&(i

%4==0); n+=leap;}
            for(i=1;i<p2->month;i++) n+=ndays[i-1];/*尾年 月->

天 */
            n+=p2->day;
            if(p2->month>2) { i=p2->year; leap=( i%400==0)||

(i%100!=0)&&(i%4==0); n+=leap;}
            if(flag==2&&n!=0&&!(p1->hour==0&&p1-

>minute==0&&p1->second==0)) n--;
        }
        if(flag>2)
        {n=n*24+(p2->hour-p1->hour);
        if(flag==3&&n!=0&&!(p1->minute==0&&p1->second==0)) n--;
        }
        if(flag>3)
        {n=n*60+(p2->minute-p1->minute);
        if(flag==4&&n!=0&&p1->second!=0) n--;
        }
        if(flag>4) n=n*60+(p2->second-p1->second);
        printf("%d/n",n/m);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值