当前位置:   article > 正文

蓝桥杯——日期问题_#includeusing namespace std;bool isleapy

#includeusing namespace std;bool isleapyear(int year){ bool bl

日期问题:大小月,平闰年

2017-7-日期问题

若干混乱的历史文献,日期在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  

 1.常规日期运算大小月(30天/31天),平年/闰年,

 2.细心

 3.字符串处理

思路:逻辑上不复杂,但是要注意细节

1 把输入的字符串切割为三个整数(年月日)

2.日、月合法性检验(大小月,平闰年),不合法返回空字符串 

3.月,日如果少零,则补全 ( 首先把整数化为字符串 i2s(),用字符串的长度判断是否少0 ) 

4.用集合 set<string> 对三种 case( 年/月/日,日/月/年,月/日/年 )进行排序去掉重复

代码

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<sstream>
  4. #include<set>
  5. using namespace std;
  6. bool isLeapYear(int year)
  7. {
  8. if(year%4==0&&year%100!=0 || year%400==0) return true;
  9. else return false;
  10. }
  11. void i2s(int i, string &s) //&s 传引用
  12. {
  13. stringstream ss;
  14. ss<<i;
  15. ss>>s;
  16. }
  17. string f(int a, int b, int c) //a年 b月 c日
  18. {
  19. //剔除不合法年月日,return ""
  20. if(a>=60 && a<=99) a+=1900;
  21. if(a>=0 && a<=59) a+=2000;
  22. if(b<1 || b>12) return "";
  23. if(c<1 || c>31) return "";
  24. switch(b){
  25. case 2 :
  26. if(isLeapYear(a) && c>29) return "";
  27. if(!isLeapYear(a) && c>28) return "";
  28. break;
  29. case 4 :
  30. if(c>30) return "";
  31. break;
  32. case 6 :
  33. if(c>30) return "";
  34. break;
  35. case 9 :
  36. if(c>30) return "";
  37. break;
  38. case 11 :
  39. if(c>30) return "";
  40. break;
  41. default :
  42. break;
  43. }
  44. //月,日如果少零,则补全(首先把整数化为字符串,用字符串的长度判断是否少0)
  45. string _a, _b, _c;
  46. i2s(a, _a);
  47. i2s(b, _b);
  48. i2s(c, _c);
  49. if(_b.length()==1) _b = "0" + _b;
  50. if(_c.length()==1) _c = "0" + _c;
  51. return _a+"-"+_b+"-"+_c;
  52. }
  53. int main(int argc, const char *argv[])
  54. {
  55. // 输入的字符串切割为三个整数(年月日)
  56. string in;
  57. cin>>in;
  58. int a=0, b=0, c=0;
  59. a = (in[0]-'0')*10 + (in[1]-'0');
  60. b = (in[3]-'0')*10 + (in[4]-'0');
  61. c = (in[6]-'0')*10 + (in[7]-'0');
  62. string case1 = f(a, b, c);
  63. string case2 = f(c, a, b);
  64. string case3 = f(c, b, a);
  65. //对元素排序(日期从小到大),去重——set
  66. set<string> ans;
  67. if(case1!="") ans.insert(case1);
  68. if(case2!="") ans.insert(case2);
  69. if(case3!="") ans.insert(case3);
  70. for(set<string>::iterator iter = ans.begin(); iter != ans.end(); iter++)
  71. {
  72. cout<<*iter<<endl;//* 指针
  73. }
  74. }

 (1)字符串处理

  1. 1) string in;
  2. cin>>in;
  3. int a = (in[0]-'0')*10 + (in[1]-'0');
  4. 2) if(_b.length()==1) _b = "0" + _b;
  5. 3)
  6. void i2s(int i, string &s) //&s 传引用
  7. {
  8. stringstream ss; //#include<sstream>
  9. ss<<i;
  10. ss>>s;
  11. }

(2) 集合set

  1. set<string> ans; //#include<set>
  2. //在集合中插入元素
  3. if(case1!="") ans.insert(case1);
  4. if(case2!="") ans.insert(case2);
  5. if(case3!="") ans.insert(case3);
  6. //用迭代器遍历
  7. for(set<string>::iterator iter = ans.begin(); iter != ans.end(); iter++)
  8. {
  9. cout<<*iter<<endl;//* 指针
  10. }

 

2013-1-高斯日记

枚举法模拟翻日历 (这个题可以用excel做)

记日记不注明年月日,而是用一个整数代替,比如:4210   

高斯出生于:1777年4月30日。
在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。

高斯获得博士学位的那天日记上标着:8113           请你算出高斯获得博士学位的年月日。

提交答案的格式是:yyyy-mm-dd, 例如:1980-03-21

  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4. bool isLeapYear(int year)
  5. {
  6. return (( year%4==0 && year%100!=0 )|| year%400==0 );
  7. }
  8. int main(int argc, const char *argv[])
  9. {
  10. int y = 1777;
  11. int m = 4;
  12. int d = 30;
  13. for(int i=1; i<8113 ;i++)
  14. {
  15. d++;
  16. if( d>31 && ( m==1 || m==3 || m==5 || m==7 || m==8 || m==10 ) )
  17. {
  18. m++;
  19. d = 1;
  20. }
  21. if( d>30 && ( m==4 || m==6 || m==9 || m==11 ) )
  22. {
  23. m++;
  24. d = 1;
  25. }
  26. if( isLeapYear(y) && m==2 && d>29 )
  27. {
  28. m++;
  29. d = 1;
  30. }
  31. if( !isLeapYear(y) && m==2 && d>28 )
  32. {
  33. m++;
  34. d = 1;
  35. }
  36. if( d>31 && m==12 )
  37. {
  38. y++;
  39. m = 1;
  40. d = 1;
  41. }
  42. //cout<<y<<" "<<m<<" "<<d<<" "<<endl;
  43. }
  44. cout<<y<<" "<<m<<" "<<d<<" "<<endl;
  45. }
  46. //1799 7 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/929662
推荐阅读
相关标签
  

闽ICP备14008679号