一段将秒转换为小时+分钟+秒数的小代码

发布于 2021-04-12  42 次阅读


#include<stdio.h>
#define MIN2SEC 60
#define HOU2SEC 3600
int main(void)
{
    int time,h,m,s,t;
    printf("Enter sec:");
    scanf("%d",&time);

    while(time>0)
    {

    h=time/HOU2SEC;
    t=time-h*HOU2SEC;
    m=t/MIN2SEC;
    s=time%MIN2SEC;
        printf("this sce = %d h %d m %d s\n",h,m,s);
    printf("Enter sec:");
    scanf("%d",&time);
    }

    return 0;
}

附加:

#include<stdio.h>
#define MIN2SEC 60
#define HOU2SEC 3600
#define DAY2SEC 86400
#define MON2SEC 2592000
#define YEA2SEC 31536000
int main(void)
{
    long time,h,m,s,t,y,mon,d;
    printf("Enter sec:");
    scanf("%d",&time);

    while(time>0)
    {
    y=time/YEA2SEC;
    t=time-y*YEA2SEC;
    mon=t/MON2SEC;
    t=t-mon*MON2SEC;
    d=t/DAY2SEC;
    t=t-d*DAY2SEC;
    h=t/HOU2SEC;
    t=t-h*HOU2SEC;
    m=t/MIN2SEC;
    s=t%MIN2SEC;
        printf("this sce = %dy %dmon%dday%dh %dm %ds\n",y,mon,d,h,m,s);
    printf("Enter sec:");
    scanf("%d",&time);
    }

    return 0;
}

擦肩而过的概率