经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
万年历
来源:cnblogs  作者:芷恬  时间:2018/11/20 10:37:53  对本文有异议

题目描述

实现思路

利用蔡勒公式计算

代码实现

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include<cmath>
  4. using namespace std;
  5. int main()
  6. {
  7. string input;
  8. cin>>input;
  9. int first_=input.find("-");//第一次出现"-"的位置.
  10. int last_=input.rfind("-");//最后一次出现"-"的位置.
  11. //cout<<first_<<endl;
  12. //cout<<last_;
  13. int year = atoi(input.substr(0,4).c_str());//取出年份
  14. int month= atoi(input.substr(first_+1,last_-first_-1).c_str());//取出月份
  15. int day = atoi(input.substr(last_+1).c_str());//取出天数
  16. //cout<<year<<endl;
  17. //cout<<month<<endl;
  18. //cout<<day;
  19. //利用蔡勒公式计算
  20. if(month==1||month==2){
  21. month=month+12;//1月份相当于上一年的13月,2月份相当于上一年的14月
  22. year=year-1;
  23. }
  24. int c=year/100;//取年份的前两位数
  25. int y=year%100;//取年份的后两位数
  26. int m=month;
  27. int d=day;
  28. int w=int((y+floor(y/4)+floor(c/4)-2*c+floor(26*(m+1)/10)+d-1))%7;
  29. if(w<0){
  30. w=w+7;
  31. }
  32. switch(w){
  33. case 0:
  34. cout<<"Sunday";
  35. break;
  36. case 1:
  37. cout<<"Monday";
  38. break;
  39. case 2:
  40. cout<<"Tuesday";
  41. break;
  42. case 3:
  43. cout<<"Wednesday";
  44. break;
  45. case 4:
  46. cout<<"Thursday";
  47. break;
  48. case 5:
  49. cout<<"Friday";
  50. break;
  51. case 6:
  52. cout<<"Saturday";
  53. break;
  54. }
  55. return 0;
  56. }
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号