c语言 定义函数给出任意一天对应的是星期几
时间: 2024-06-08 21:10:09 浏览: 97
以下是一个示例代码,实现了给定年月日,输出对应的星期几:
```c
#include <stdio.h>
int day_of_week(int year, int month, int day) {
if (month == 1 || month == 2) {
month += 12;
year--;
}
int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
return week;
}
int main() {
int year, month, day;
printf("请输入日期(格式:年 月 日):");
scanf("%d %d %d", &year, &month, &day);
int week = day_of_week(year, month, day);
char* week_str[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
printf("%d年%d月%d日是%s\n", year, month, day, week_str[week]);
return 0;
}
```
在这个函数中,我们使用了蔡勒公式来计算星期几。该公式的具体实现可以参考维基百科:https://zh.wikipedia.org/wiki/%E8%94%A1%E5%8B%92%E5%85%AC%E5%BC%8F。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)