C++实现——由年月日推算是星期几
2016-04-07 19:17
239 查看
#include <iostream> #include <string> #include <cmath> using namespace std; /* 语法:result = weekday(int N, int M, int d) 参数: N, M, d:年月日,例如:2003, 11, 4 返回值:0:星期天,1星期一…… 注意: 需要cmath 适用于1582年10月15日之后, 因为罗马教皇格里高利十三世在这一天启用新历法. */ //给定年月日,输出当天是星期几 int weekday(int N, int M, int d) { int m, n, c, y, w; m = (M - 2) % 12; if (M >= 3) n = N; else n = N - 1; c = n / 100; y = n % 100; w = (int)(d + floor(13 * m / 5) + y + floor(y / 4) + floor(c / 4) - 2 * c) % 7; while (w<0) w += 7; return w; } //测试函数 int main(){ int year, month, day; string week[] = {"日","一","二","三","四","五","六"}; while (cin >> year >> month >> day){ cout << year << "年" << month << "月" << day << "日是星期" << week[weekday(year, month, day)] << endl; } }
相关文章推荐
- 关于C语言的问卷调查
- .ipp文件是做什么的
- C++实现——排列组合
- 表达式计算器(逆波兰法)栈操作(C语言实现)
- 关于C语言的问卷调查
- 关于C语言的问卷调查
- 门禁系统
- c语言基础概念(3)
- c语言基础概念(2)
- c语言基本概念
- 【步兵 c++】vector 的 去重
- C++设计模式<三>:模板方法(Template Method 模式)
- C++11新特性,利用std::chrono精简传统获取系统时间的方法
- (C++) LNK2019: unresolved external symbol.
- 快速上手,协程剖析
- CSP考试 2014年12月第4题 C语言实现
- C语言—局部变量同名初始化问题
- C++上机实验3-4
- C语言之malloc()函数
- VS2008非托管C++调用wcf(WebService)服务