返回题解分享
讨论 / 题解分享/ 帖子详情

日期问题(编程题) - 题解

#include <bits/stdc++.h>
using namespace std;
int m1[]={0,1,3,5,7,8,10,12};
int m2[]={0,4,6,9,11};
string trans(int year,int month,int day){

string ans="";
 if(year>=0&&year<=59){
    ans+="20";
    if(year<10){
    ans+="0";
}

}
else if(year>59){
ans+="19";
}

ans+=to_string(year)+"-";


//处理月份
if(month<10){
    ans+="0";
}
ans+=to_string(month)+"-";
//处理日期
if(day<10){


ans+="0";
}
ans+=to_string(day);

int day1=stoi(ans.substr(8,2));
int month1=stoi(ans.substr(5,2));
int year1=stoi(ans.substr(0,4));


// cout<<year1<<" "<<month1<<" "<<day1<<endl;

if(month1>12||month1==0||day1==0){
    //  cout<<" 111"<<endl;
    return " ";
  }

for(int i=1;i<=7;i++){
    if(month1==m1[i]&&day1>31){


// cout<<" 222"<<endl;
return " ";
}
}

for(int i=1;i<=5;i++){
    if(month1==m2[i]&&day1>30){
        //   cout<<month1<<" "<<day1<<endl;
        //  cout<<" 333"<<endl;
        return " ";
    }
}
 if(month1==2){
    //判断是闰年还是平年
    if(year1%400==0||(year1%4==0&&year1%100!=0)){
         if(day1>29){
            //  cout<<" 444"<<endl;
            return " ";
         }
    }
    else{
        if(day1>28){
            //  cout<<" 555"<<endl;
            return" "; 
        }
    }
 }

//    cout<<" 666"<<endl;
 return ans;


}

int main(){
// 01,34,67
vector<string> a;
string data;
cin>>data;
string f1 = data.substr(0, 2); // 年
string f2 = data.substr(3, 2); // 月
string f3 = data.substr(6, 2); // 日

int year,day,month;

//先按照年月日
year=stoi(f1);
month=stoi(f2);
day=stoi(f3);
// cout<<year<<" "<<month<<" "<<day<<endl;

string k1=trans(year,month,day);
string k2=trans(day,year,month);
string k3=trans(day,month,year);

a.push_back(k1);
a.push_back(k2);
a.push_back(k3);

sort(a.begin(),a.end());
for(int i=0;i<a.size();i++){
    if(a[i]!=" "){
        if(i==0){
            cout<<a[i]<<endl;
        }
        else
             if(a[i]!=a[i-1]){
            cout<<a[i]<<endl;
        }
       
    }
}



return 0;


}
0 回复 0 转发 0 喜欢 3 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!