日期问题c++

原文地址

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

小明知道这些日期都在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 <xxx>不能通过工程设置而省略常用头文件。

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

#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值