pta日期几何

关于日期的结构定义如下:
struct DateG{ int yy,mm,dd;};

编写两个函数,一个计算自公元1年1月1日到指定的日期共经历了多少天。另一个是前一个函数的逆函数:由自公元1年1月1日历经指定的天数后的日期(返回年月日构成的日期)。

函数接口定义:

 
struct DateG days2Date(int x);{//from:{1,1,1} + 100-->{1,4,11}

int Days(DateG x);// {2,,4,11} return 465 from{1,1,1}

裁判测试程序样例:

 
struct DateG{ int yy,mm,dd; };

char leapYear(int year);//闰年否

struct DateG _DateG(char *st);{//"1919-9-9"-->{1919,9,9}

struct DateG Add(struct DateG x,int y);//{1919,9,9}+50-->{1919,10,29}

char* Date2string(struct DateG x);//{1919,9,9}-->"1919-09-09" //以上为测试程序提供的测试用函数,略去不表, //以下两函数是必须自己完成的函数

struct DateG days2Date(int x);{//from:{1,1,1} + 100-->{1,4,11}

int Days(struct DateG x);// {2,,4,11} return 465 from{1,1,1}

int main(){

char st[12];

scanf("%s",st);

struct DateG d1=_DateG(st), d2=Add(d1,60*365);

printf("%s\n",Date2string(d2));

d2=Add(d2,-1000);

printf("%s\n",Date2string(d2));

d2=_DateG("2020-1-23");

printf("%.2f\n",(Days(d2)-Days(d1))/365.0);

return 0;

} /* 请在这里填写答案 */

输入样例:

输入用-号分隔的出生年月日,系统确保测试数据的合法性

2001-1-1

输出样例:

共三行输出。分别是:60年(按365天计一年精确计算的)后的退休日期;退休前倒计时1000天的日期;武汉抗疫封城时(2020-1-23)的年龄(按每365天为一岁,保留2位小数)。

[2060-12-17]
[2058-03-23]
19.07

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

参考代码如下:

int isyear(int a)//wsy142
{
    return ((a%400==0)||(a%100!=0&&a%4==0))?1:0;
}
struct DateG days2Date(int x)//from:{1,1,1} + 100-->{1,4,11}
{
    int n;
    struct DateG T;
    T.yy=1;
    T.mm=1;
    T.dd=1;
    //n=isyear(T.yy);//0||1
    while(x>(365+isyear(T.yy)))//先确定年份
    {
        x=x-365-isyear(T.yy);
        T.yy++;
    }
    n=isyear(T.yy);
    int a[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};
 
    while(x>a[n][T.mm])
    {
        x=x-a[n][T.mm];
        T.mm++;
    }
    T.dd+=x;
    return T;
}
int Days(struct DateG x)// {2,4,11}    return 465   from{1,1,1}
{
    int sum=0, i=1;
    while(x.yy!=i)//
    {        
            sum+=(365+isyear(i++));
    }
    i=1;
    int a[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    while(x.mm!=i)
    {
        if(i==2)
        {
            sum+=(28+isyear(x.yy));
        }
        else
        {
            sum+=a[i];
        }
        i++;
    }
    sum+=x.dd-1;
  return sum;
}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值