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
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.
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;
}
說實話這一題不算難,問題就是題意真的不好理解。。
相關文章
- TIMESTAMP的時區轉換
- 時間戳與時間字串的多時區轉換時間戳字串
- 使用 Go 語言進行多時區時間轉換Go
- 用datetime和pytz來轉換時區
- 處理日期和時區轉換:為什麼正確的 UTC 轉換很重要
- Rust中如何將本地時間轉換為另一個時區?Rust
- 使用pytz模組進行時區轉換及時間計算
- 時間轉換
- 時間轉換成時間戳時間戳
- C 時間轉換時間戳時間戳
- 掌握時間與空間:深入探討Golang中的時間戳與時區轉換Golang時間戳
- MSSQL-從字串轉換日期和/或時間時,轉換失敗SQL字串
- [時間格式的轉換]
- Java之時間轉換Java
- java時間的轉換Java
- 轉換UTC時間格式
- 工具方法:時間轉換
- MySQL:一次timestamp時區轉換導致的問題MySql
- C 時間戳轉換成時間時間戳
- 時間相減和時間轉換
- C#操作時區轉換時遇到的一些問題和解決方法分享C#
- Oracle資料庫的SCN轉換成時間和時間轉換成SCNOracle資料庫
- (轉)CWnd與HWND的區別與轉換
- JavaScript 轉換成UTC時間JavaScript
- JavaScript 時間日期格式轉換JavaScript
- Mysql 查詢時間轉換MySql
- c++時間形式轉換C++
- unix時間轉換為datetimedatetime轉換為unixtime
- 定時器時間資料轉換子程式分析 (轉)定時器
- Python秒轉換成時間(時分秒)Python
- 時間日期字串轉換為時間物件字串物件
- utc時間轉換成標準時間
- Gson轉換與JSONObject區別JSONObject
- Oracle 12CR2查詢轉換之臨時錶轉換Oracle
- JavaScript 時間日期轉換成中文JavaScript
- Excel中時間戳轉換時間Excel時間戳
- Timestamp-時間戳轉換時間戳
- JavaScript 時間轉換為UTC格式JavaScript