侧边栏壁纸
博主头像
王小木人

这是很长,很好的一生

  • 累计撰写 141 篇文章
  • 累计创建 43 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

日期问题c++

王小木人
2021-05-22 / 0 评论 / 0 点赞 / 942 阅读 / 2,226 字

标题:日期问题小明正在整理一批历史文献。这些历史文献中出现了很多日期。

小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,

有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,

使得文献上的一个日期,存在很多可能的日期与其对应。 比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?输入----一个日期,

格式是"AA/BB/CC"。 (0 <= A, B, C <= 9) 输出----输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。

多个日期按从早到晚排列。 样例输入----02/03/04 样例输出----2002-03-04 2004-02-03 2004-03-02

资源约定:峰值内存消耗(含虚拟机) < 256MCPU消耗 < 1000ms请严格按要求输出,不要画蛇添足地打印类似:

“请您输入...” 的多余内容。注意:main函数需要返回0;只使用ANSI C/ANSI C++ 标准;不要调用依赖于编译环境或操作系统的特殊函数。

所有依赖的函数必须明确地在源文件中 #include 不能通过工程设置而省略常用头文件。

提交程序时,注意选择所期望的语言类型和编译器类型。

#include<iostream>

#include<string.h>
#include<algorithm>
#include<iterator>
#include<sstream>
#include<set>
using namespace std;
int main()
{
    string strdate;
    cin>>strdate;
    string datearry[3];
    datearry[0]=strdate.substr(0,2);
    datearry[1]=strdate.substr(3,2);
datearry[2]=strdate.substr(6,2);
  set<string> dite;
 
    bool istruedate(string,string,string);
   
    if(istruedate("19"+datearry[0],datearry[1],datearry[2]))
      dite.insert("19"+datearry[0]+"-"+datearry[1]+"-"+datearry[2]);
    
    
    if(istruedate("19"+datearry[2],datearry[0],datearry[1]))
      dite.insert("19"+datearry[2]+"-"+datearry[0]+"-"+datearry[1]);
    
    
    if(istruedate("19"+datearry[2],datearry[1],datearry[0]))
    
      dite.insert("19"+datearry[2]+"-"+datearry[1]+"-"+datearry[0]);
    
    if(istruedate("20"+datearry[0],datearry[1],datearry[2]))
    
    dite.insert("20"+datearry[0]+"-"+datearry[1]+"-"+datearry[2]);
    if(istruedate("20"+datearry[2],datearry[0],datearry[1]))
    
    dite.insert("20"+datearry[2]+"-"+datearry[0]+"-"+datearry[1]);
    if(istruedate("20"+datearry[2],datearry[1],datearry[0]))
    
         dite.insert("20"+datearry[2]+"-"+datearry[1]+"-"+datearry[0]);
   set<string>::iterator itestr=dite.begin();
  while(itestr!=dite.end())
  
  {
  cout<<*itestr<<'\n';
  itestr++;
  }
return 0;
}


bool istruedate(string syear,string smonth,string sday)
{
int year,month,day;
   stringstream stream;
    stream<<syear;
    
stream>>year;
stream.clear();
stream<<smonth;
    
stream>>month;
stream.clear();
stream<<sday;
    
stream>>day;
stream.clear();


if(year<1960||year>2059)
return false;
if(month>12||day>31||month<=0||day<=0) 
return false;
int maxday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if((year%4==0&&year%100!=0)||year%400==0)
{
maxday[1]++;
}
if(maxday[month-1]>=day) 
{
return true;
}
else

{
return false;
}


}
0

评论区