日期与时间问题

1.日期问题:根据月份和日期计算天数,这里的num表示细菌繁殖的初始数目。

#include 
void main()
{
    int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int n,i,j,days;
    int mon1,date1,mon2,date2,num1;
    long num;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d%d%d",&mon1,&date1,&num1,&mon2,&date2);
        days = date2-date1;
        for(i=mon1;i

2.日历问题:输出年月日和星期几,星期可以用%7来计算,年月日要分别计算,注意最后dyas是否+1要看题目要求。

#include 

char week[7][10]={"Saturday","Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday"};
int year[2] = {365,366};
int month[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},
{31,28,31,30,31,30,31,31,30,31,30,31}};

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

void main()
{
   int days,numWeek;
   int i=0,j=0;
   while(scanf("%d",&days) && days !=-1)
   {
       numWeek = days % 7;
       for(i=2000;days>=year[type(i)];i++)
           days -= year[type(i)];
       for(j=0;days>=month[type(i)][j];j++)
           days -= month[type(i)][j];
       printf("%d-%02d-%02d %s\n",i,j+1,days+1,week[numWeek]);
   }
}

3.时区间的时间转换
(1)计算两个时区之间的时间差,以分钟为单位。通过比较字符串的方式来找到输入的时区是哪一个,然后时区对应着一个时间差别。最后新的时间要统一到同一天。
(2)如何输入,全部用hours和minutes来存时间。
(3)输出,按格式输出hours和minutes。

#include 
#include 

int difference(char *Zone1,char *Zone2)//计算时区间的时差
{
    char *Zone[32] = {"UTC","GMT","BST","IST","WET","WEST",
                      "CET","CEST","EET","EEST","MSK",
                      "MSD","AST","ADT","NST","NDT",
                      "EST","EDT","CST","CDT","MST",
                      "MDT","PST","PDT","HST","AKST",
                      "AKDT","AEST","AEDT","ACST","ACDT",
                      "AWST"};
    float time[32] = {0,0,1,1,0,1,1,2,2,3,3,4,-4,-3,-3.5,-2.5,-5,-4,-6,-5,-7,
        -6,-8,-7,-10,11,9.5,10.5,8};
    int i,j;
    for(i=0;strcmp(Zone1,Zone[i]);i++);
    for(j=0;strcmp(Zone2,Zone[j]);j++);
    return (int)(time[i]-time[j])*60;
}




void main()
{
    int nCase,hours,minutes,newTime;
    char Zone1[5],Zone2[5];
    char time[11];
    scanf("%d",&nCase);
    while(nCase--)
    {
       scanf("%s",time);
       switch (time[0])
       {
       case 'n':hours=12;minutes=0;break;
       case 'm':hours=0;minutes=0;break;
       default:
           sscanf(time,"%d:%d",&hours,&minutes);//提取hours和minutes
           hours %= 12;//这一句必须要!!!!
           scanf("%s",time);
           if(time[0]=='p') hours += 12;
       }

       scanf("%s%s",Zone1,Zone2);
       newTime = 60*hours + minutes + difference(Zone2,Zone1);
       if(newTime<0) newTime += 1440;//提前一天,则加上一天
       else newTime %=1440;

       switch(newTime)
       {
       case 0: printf("midnight\n");
               break;
       case 720: printf("noon\n");
                 break;
       default:hours = newTime/60;
               minutes = newTime%60;
               if(hours ==0)
                   printf("12:02d a.m.\n",minutes);
               else if(hours<12)
                   printf("%d:%02d a.m.\n",hours,minutes);
               else if(hours==12)
                   printf("12:%02d p.m.\n",minutes);
               else
                   printf("%d:%02d a.m.\n",hours%12,minutes);
       }
    }
}

你可能感兴趣的:(日期与时间问题)