博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
蓝桥杯 2017_7 日期问题(c与c++知识学习)
阅读量:4217 次
发布时间:2019-05-26

本文共 3895 字,大约阅读时间需要 12 分钟。

第七题

标题:日期问题小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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 
不能通过工程设置而省略常用头文件。提交程序时,注意选择所期望的语言类型和编译器类型。a

虽然我的方法思路和大神的类似,但是别人的处理方法确实是精简干练,这源于对知识的掌握与运用
比如读取数据的部分,没想到直接scanf("%d/%d/%d")这样就可以
还有下面的结构体直接根据题意进行初始化,真是妙。

我的方法:
#include
#include
#include
#include
using namespace std;string y[2] = {"19","20"};const int day1[14] = {0,31,28,31,30,31,30,31,31,30,31,30,31};const int day2[14] = {0,31,29,31,30,31,30,31,31,30,31,30,31};int isL(int y) { return (y%400 == 0 || (y%4 == 0 && y%100 != 0)); }bool judge(int y, int m, int d) { if(y < 1960 || y >= 2060) return false; if(m < 1 || m > 12) return false; if(isL(y)){ if(d > day2[m] || d <= 0) return false; } else{ if(d > day1[m] || d <= 0) return false; } return true; }int cal(string ss)//转换为10进制 { int num = 0; for(int i = 0; i < ss.size(); ++i){ num = num*10+(ss[i]-'0'); } return num; }vector
split(const string &s,const string &sep)//分隔符读取 { vector
res; int end = s.find(sep); int st = 0; while(string::npos != end){ res.push_back(s.substr(st,end-st)); st = end + sep.size(); end = s.find(sep,st); } if(st != s.length()){ res.push_back(s.substr(st)); } return res; }int main() { string ss; cin >> ss; vector
sp = split(ss,"/"); vector
res; string tmp = ""; int t; //处理 年、月、日 for(int i = 0; i < 2; ++i){ tmp = ""; if(!judge(cal(y[i]+sp[0]),cal(sp[1]),cal(sp[2]))) continue; tmp = tmp+y[i] + sp[0] + sp[1] + sp[2]; res.push_back(tmp); } //处理 日、月、年 for(int i = 0; i < 2; ++i){ tmp = ""; if(!judge(cal(y[i]+sp[2]),cal(sp[1]),cal(sp[0]))) continue; tmp = tmp +y[i]+ sp[2] + sp[1] + sp[0]; res.push_back(tmp); } //处理 月、日、年 for(int i = 0; i < 2;++i){ tmp = ""; if(!judge(cal(y[i]+sp[2]),cal(sp[0]),cal(sp[1]))) continue; tmp = tmp +y[i]+ sp[2] + sp[0] + sp[1]; res.push_back(tmp); } sort(res.begin(),res.end());//string对象按照字典序 int cnt = 0; for(int i = 0; i < res.size(); ++i){ string str = res[i]; cnt = 0; for(int j = 0; j < str.size(); ++j){ cout << str[j]; if(cnt == 3){ cout<<"-"; } if(cnt == 5) cout <<"-"; ++cnt; } cout <

大神的:
#include 
#include
#include
using namespace std;typedef struct{ int year, month, day;}date;bool isyn(int y){ return (y % 4 == 0) || (y % 100 && y % 400 == 0);}void print(const date &d){ printf("%02d-%02d-%02d\n", d.year, d.month, d.day);}bool compare(const date &d1, const date &d2){ if(d1.year != d2.year){ return d1.year < d2.year; } if(d1.month != d2.month){ return d1.month < d2.month; } return d1.day < d2.day;}bool check(const date &d){ static int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(isyn(d.year)){ month_days[2] = 29; }else{ month_days[2] = 28; } if(d.year < 1960 || d.year > 2059){ return false; } if(d.month < 1 || d.month > 12){ return false; } if(d.day < 1 || d.day > month_days[d.month]){ return false; }}int main(){ int aa, bb, cc; scanf("%d/%d/%d", &aa, &bb, &cc); date d[6] = { {2000 + aa, bb, cc}, {1900 + aa, bb, cc}, {2000 + cc, aa, bb}, {1900 + cc, aa, bb}, {2000 + cc, bb, aa}, {1900 + cc, bb, aa} }; sort(d, d + 6, compare); for(int i = 0; i < 6; ++ i){ if(check(d[i])){ print(d[i]); } } return 0;}

转载地址:http://uhimi.baihongyu.com/

你可能感兴趣的文章
CSS之Multi-columns的column-gap和column-rule
查看>>
CSS之Multi-columns的跨列
查看>>
CSS之浮动(一)
查看>>
CSS之浮动(二)
查看>>
AtomicInteger源码解析
查看>>
CopyOnWriteArraySet源码学习
查看>>
Openfiler 配置 NFS 示例
查看>>
Oracle 11.2.0.1 RAC GRID 无法启动 : Oracle High Availability Services startup failed
查看>>
Oracle 18c 单实例安装手册 详细截图版
查看>>
Oracle Linux 6.1 + Oracle 11.2.0.1 RAC + RAW 安装文档
查看>>
Oracle 11g 新特性 -- Online Patching (Hot Patching 热补丁)说明
查看>>
Oracle 11g 新特性 -- ASM 增强 说明
查看>>
Oracle 11g 新特性 -- Database Replay (重演) 说明
查看>>
Oracle 11g 新特性 -- 自动诊断资料档案库(ADR) 说明
查看>>
Oracle 11g 新特性 -- RMAN Data Recovery Advisor(DRA) 说明
查看>>
CSDN博客之星 投票说明
查看>>
Oracle wallet 配置 说明
查看>>
Oracle smon_scn_time 表 说明
查看>>
VBox fdisk 不显示 添加的硬盘 解决方法
查看>>
Secure CRT 自动记录日志 配置 小记
查看>>