POJ-1446 Moscow Time-时区转换

Moscow Time
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 392 Accepted: 96

Description

In e-mail the following format for date and time setting is used:

EDATE::=Day_of_week, Day_of_month Month Year Time Time_zone


Here EDATE is the name of date and time format, the text to the right from ``::=" defines how date and time are written in this format. Below the descriptions of EDATE fields are presented:

Day-of-week

The name of a day of the week. Possible values: MON, TUE, WED, THU, FRI, SAT, SUN. The name is followed by ``," character (a comma).

Day-of-month

A day of the month. Set by two decimal digits.

Month

The name of the month. Possible values: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC.

Year

Set by two or four decimal digits. If a year is set by two decimals it is assumed that this is a number of the year of the XX century. For instance, 74 and 1974 set a year of 1974.

Time

Local time in format hours:minutes:seconds, where hours, minutes and seconds are made up of two decimal digits. The time keeps within the limits from 00:00:00 to 23:59:59.

Time-zone

Offset of local time from Greenwich mean time. It is set by the difference sign ``+" or ``-"and by sequence of four digits. First two digits set the hours and the last two the minutes of offset value. The absolute value of the difference does not exceed 24 hours. Time zone can also be presented by one of the following names:

Name Digital value
UT   -0000 

GMT  -0000 

EDT  -0400 

CDT  -0500 

MDT  -0600 

PDT  -0700 


Each two adjacent fields of EDATA are separated with exactly one space. Names of day of the week, month and time zone are written in capitals. For instance, 10 a.m. of the Contest day in St.Petersburg can be presented as

TUE, 03 DEC 96 10:00:00 +0300

Write a program which transforms the given date and time in EDATE format to the corresponding date and time in Moscow time zone. So called ``summer time" is not taken into consideration. Your program should rely on the predefined correctness of the given Day-of-week and Time-zone.

A note

Moscow time is 3 hours later than Greenwich mean time (time zone +0300)
Months: January, March, May, July, August, October and December have 31 days. Months: April, June, September and November have 30 days. February, as a rule, has 28 days, save for the case of the leap year (29 days).

A year is a leap year if valid one out of two following conditions:
its number is divisible by 4 and is not divisible by 100;
its number is divisible by 400.
For instance, 1996 and 2000 are the leap years, while 1900 and 1997 are not.

Input

Input contains date and time in EDATE format in the first line. Minimum permissible year in the input data is 0001, maximum - 9998. Input EDATA string does not contain leading and trailing spaces.

Output

Output must contain a single line with date and time of Moscow time zone in EDATE format. In output EDATE string a Year can be presented in any of the two allowed ways. The output string should not include leading and trailing spaces.

Sample Input

SUN, 03 DEC 1996 09:10:35 GMT

Sample Output

SUN, 03 DEC 1996 12:10:35 +0300

Source


//木有AC,可能是题意理解的不好。
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
using namespace std;
const char w[7][20]= {"SUN","MON","TUE","WED","THU","FRI","SAT"};
const char m[12][20]= {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
const char na[6][10]= {"UT","GMT","EDT","CDT","MDT","PDT"};
const char t[6][10]= {"+0300","+0300","-0100","-0200","-0300","-0700"};
const int tt[6]= {3,3,7,8,9,10};
int daysofmonth(int m,int y)
{
    int days=0;
    switch(m)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        days=31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        days=30;
        break;
    case 2:
        if((y%4==0&&y%100!=0)||y%400==0)
            days=29;
        else
            days=28;
    }
    return days;
}
int main()
{
    string week,tn,month;
    int day,year,hour,minute,second,i=0,flag=0,dh,dm;
    char c1,c2,time[5];
    getline(cin,week,',');
    cin>>day>>month>>year>>hour>>c1>>minute>>c2>>second>>tn;
    cin>>time;
    int ti,n,a[4];
    ti=atoi(time);//将时区时间转换为整型
    n=ti;
    //cout<<week<<day<<month<<year<<hour<<c1<<minute<<c2<<second<<tn<<ti<<endl;
    memset(a,0,sizeof(a));
    while(n>0)
    {
        a[i]=n%10;
        n/=10;
        ++i;
    }
    dh=a[3]*10+a[2];//计算出对应格林尼治时间时间的差值
    dm=a[1]*10+a[0];
    int mi;
    for(i=0; i<12; ++i)
        if(month==m[i])
            mi=i;
    int wi;//计算第几周
    for(i=0; i<7; ++i)
        if(week==w[i])
            wi=i;
    int tti;
    for(i=0; i<6; ++i)//判断是否为给定时区内的
        if(tn==na[i])
        {
            hour+=tt[i];
            flag=1;
            tti=i;
        }
    if(flag==0)
    {
        if(ti<0)
        {
            hour-=dh;
            minute+=dm;
        }
        else
        {
            hour+=dh;
            minute-=dm;
        }
    }
    //cout<<hour<<" "<<minute<<endl;
    if(minute<0)
    {
        minute+=60;
        --hour;
        //cout<<hour<<" "<<minute<<endl;
    }
    else if(minute>=60)
    {
        minute-=60;
        ++hour;
        //cout<<hour<<" "<<minute<<endl;
    }
    if(hour<0)
    {
        hour+=24;
        wi=(wi-1+7)%7;
        --day;
        if(day<=0)
        {
            --mi;
            if(mi<1)
            {
                mi=12;
                --year;
                day=daysofmonth(mi,year);
            }
        }
        //cout<<hour<<" "<<minute<<endl;
    }
    else if(hour>=24)
    {
        hour-=24;
        wi=(wi+1)%7;
        ++day;
        if(day>daysofmonth(mi,year))
        {
            day=1;
            ++mi;
            if(mi>12)
            {
                mi=1;
                ++year;
            }
        }
        //cout<<hour<<" "<<minute<<endl;
    }
    cout<<w[wi]<<", ";
    printf("%02d ",day);
    cout<<m[mi]<<" "<<year<<" ";
    printf("%02d%c%02d%c%02d ",hour,c1,minute,c1,second);
    if(flag==1)
        cout<<t[tti]<<endl;
    else
        cout<<time<<endl;
    return 0;
}

下面贴一个AC了的代码:

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
using namespace std;
char WDAY[7][4]  = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
char MON[12][4]  = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
char MON_DAY[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char ZONE[6][4]   = { "UT", "GMT", "EDT", "CDT", "MDT", "PDT" };
int ZONE_TIME[6] = { 0, 0, -400, -500, -600, -700 };
struct mydate
{
    int sec, min, hour;
    int mday, mon, year, wday;
    int zone;
    void changezone(int zone2)
    {
        int zonehh = zone/100, zonemm = zone%100;
        int zone2hh = zone2/100, zone2mm = zone2%100;
        zone = zone2;
        if( zone2mm<zonemm ) zone2hh--, zone2mm+=60;
        hour += zone2hh-zonehh, min += zone2mm-zonemm;
        if( min>=60 ) min%=60, ++hour;
        if( hour>=24 ) ++mday, ++wday, hour-=24;
        else if( hour<0 ) hour+=24, --mday, --wday;
        if( wday>=7 ) wday-=7;
        else if( wday<0 ) wday+=7;

        if( mday<=0 )
        {
            --mon;
            if( mon<0 ) mon+=12, --year;
            bool leap = false;
            if( (year%4==0 && year%100!=0) || (year%400==0 ) ) leap = true;
            int thismon = MON_DAY[mon]+(int)(mon==1&&leap);
            mday += thismon;
        }
        else
        {
            bool leap = false;
            if( (year%4==0 && year%100!=0) || (year%400==0 ) ) leap = true;
            int thismon = MON_DAY[mon]+(int)(mon==1&&leap);
            if( mday>thismon ) mday-=thismon, ++mon;
            if( mon>=12 ) mon-=12, ++year;
        }
    }
    void print()
    {
        printf("%s, %02d %s %04d %02d:%02d:%02d %+05d\r\n", WDAY[wday], mday, MON[mon], year, hour, min, sec, zone);
    }
} xd;
int main()
{
    char s[10];
    while( ~scanf("%s", s) )
    {
        s[3] = '\0';
        for(int i=0; i<7; ++i)
            if( !strcmp(s, WDAY[i]) )
            {
                xd.wday = i;
                break;
            }
        scanf("%d", &xd.mday);
        scanf("%s", s);
        for(int i=0; i<12; ++i)
            if( !strcmp(s, MON[i]) )
            {
                xd.mon = i;
                break;
            }
        scanf("%s", s);
        xd.year = atoi(s);
        if( strlen(s)<=2 ) xd.year+=1900;
        scanf("%2d:%2d:%2d", &xd.hour, &xd.min, &xd.sec);
        scanf("%s", s);
        if( s[0]=='+' || s[0]=='-' )
            sscanf(s, "%d", &xd.zone);
        else
        {
            for(int i=0; i<6; ++i)
                if( !strcmp(s, ZONE[i]) )
                {
                    xd.zone = ZONE_TIME[i];
                    break;
                }
        }
        xd.changezone(300);
        xd.print();
    }

    return 0;
}

说实话这一题不算难,问题就是题意真的不好理解。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值