1323.cpp

来自「ZOJ 动态规划算法题目入门与提高 源代码」· C++ 代码 · 共 168 行

CPP
168
字号
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const string _y="19";
bool nv(int y){
	if(y%400==0) return 1;
	else if(y%100==0) return 0;
    else if(y%4==0) return 1;
	else return 0;
}
/*
int mdata(const data & a,int m){
    switch (m){
	case 1: return 31;
	case 2: {if(nv(a.y)) return 29; else return 28;}
    case 3: return 31;
	case 4: return 30;
	case 5: return 31;
	case 6: return 30;
	case 7: return 31;
	case 8: return 31;
	case 9: return 30;
	case 10: return 31;
	case 11: return 30;
	case 12: return 31;
	}
}
*/
string getmosstring (int a){
	if(a==1) return "JAN"; 
	if(a==2) return "FEB";
	if(a==3) return "MAR";
	if(a==4) return "APR";
	if(a==5) return "MAY";
	if(a==6) return "JUN";
	if(a==7) return "JUL";
	if(a==8) return "AUG";
	if(a==9) return "SEP";
	if(a==10) return "OCT";
	if(a==11) return "NOV";
	if(a==12) return "DEC";
}
int getmos(string s){
	if(s=="JAN") return 1;
	if(s=="FEB") return 2;
	if(s=="MAR") return 3;
	if(s=="APR") return 4;
	if(s=="MAY") return 5;
	if(s=="JUN") return 6;
	if(s=="JUL") return 7;
	if(s=="AUG") return 8;
	if(s=="SEP") return 9;
	if(s=="OCT") return 10;
	if(s=="NOV") return 11;
	if(s=="DEC") return 12;
}
string cov(string week,int a){
    if(a==0) return week;
	if(a==1) {
	   if(week=="MON,") return "TUE,";
	   if(week=="TUE,") return "WED,";
	   if(week=="WED,") return "THU,";
	   if(week=="THU,") return "FRI,";
	   if(week=="FRI,") return "SAT,";
	   if(week=="SAT,") return "SUN,";
	   if(week=="SUN,") return "MON,";
	}
	if(a==-1){
	   if(week=="MON,") return "SUN,";
	   if(week=="TUE,") return "MON,";
	   if(week=="WED,") return "TUE,";
	   if(week=="THU,") return "WED,";
	   if(week=="FRI,") return "THU,";
	   if(week=="SAT,") return "FRI,";
	   if(week=="SUN,") return "SAT,";
	}
}
void deal(int & y,int & m,int & d,int a){
    if(a==0) return;
	if(a==-1){
         if(d!=1) d-=1;
         else{
            if(m==12||m==10||m==7||m==5) {d=30;m-=1;}
            else if(m==11||m==9||m==8||m==6||m==4||m==2){d=31;m-=1;}
            else if(m==1) {m=12;d=31;y-=1;}
            else{
                 if(nv(y)) d=29; else d=28; 
		         m-=1;
			}
		 }
		 return;
	}
	if(a==1){
		if(m==1||m==3||m==5||m==7||m==8||m==10){
              if(d!=31) d+=1;
			  else{d=1;m+=1;}
		}
		else if(m==4||m==6||m==9||m==11){
              if(d!=30) d+=1;
			  else{d=1;m+=1;}
		}
		else if(m==12){
              if(d!=31) d+=1;
			  else{d=1;m=1;y+=1;}
		}
		else{
             if(d<28) d+=1;
			 else if(d==29) {d=1;m+=1;}
			 else{
                 if(nv(y)) d+=1;
				 else {d=1;m+=1;}
			 }
		}
	}
}
string yearstring(int y){
      string s="0000";
	  int i=3;
	  while(y!=0){
         s[i]=char(y%10+'0');
		 y/=10;
		 i-=1;
	  }
	  return s;
}
string datastring(int y){
      string s="00";
	  int i=1;
	  while(y!=0){
         s[i]=char(y%10+'0');
		 y/=10;
		 i-=1;
	  }
	  return s;
}
int main(){
	//ifstream cin("in.txt");
    string week,day,mos,year,time,zone;
	while(cin>>week>>day>>mos>>year>>time>>zone){
        if(zone=="UT"||zone=="GMT") zone="-0000";
		else if(zone=="EDT") zone="-0400";
		else if(zone=="CDT") zone="-0500";
		else if(zone=="MDT") zone="-0600";
		else if(zone=="PDT") zone="-0700";
		else {}
		if(year.length()==2) year.insert(year.begin(),_y.begin(),_y.end());
		int y=(year[0]-'0')*1000+(year[1]-'0')*100+(year[2]-'0')*10+(year[3]-'0'),m=getmos(mos),d=(day[0]-'0')*10+(day[1]-'0');
	    int h=(time[0]-'0')*10+(time[1]-'0'),f=(time[3]-'0')*10+(time[4]-'0'),s=(time[6]-'0')*10+(time[7]-'0');
		int zonenum=((zone[1]-'0')*10+(zone[2]-'0'))*60+(zone[3]-'0')*10+(zone[4]-'0');
		if(zone[0]=='-'){zonenum=(-zonenum);}
		int zoneadd=3*60-zonenum,timenum=h*3600+f*60+s+zoneadd*60;
		int daychange=0;
		if(timenum<0) {timenum+=3600*24;daychange=-1;}
		if(timenum>=3600*24) {timenum-=3600*24;daychange=1;}
		h=timenum/3600;f=(timenum-h*3600)/60;s=timenum-h*3600-f*60;
		//cout<<timenum<<endl;
		//cout<<h<<" "<<f<<" "<<s<<" "<<daychange<<endl;
		week=cov(week,daychange);
		deal(y,m,d,daychange);
		cout<<week<<" ";
		if(d<10) cout<<"0"<<d<<" ";
		else cout<<d<<" ";
		cout<<getmosstring(m)<<" "<<yearstring(y)<<" "<<datastring(h)<<":"<<datastring(f)<<":"<<datastring(s)<<" +0300";
		cout<<endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?