用c语言解决E. 日期计算 【问题描述】 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有29天。满足下面条件之一的是闰年: 1) 年份是4的整数倍,而且不是100的整数倍; 2) 年份是400的整数倍。 【输入形式】 输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。 输入的第二行包含一个整数d,d在1至365之间。 【输出形式】 输出两行,每行一个整数,分别表示答案的月份和日期。 【样例输入】 2015 80 【样例输出】 3 21 【样例输入】 2000 40 【样例输出】 2 9
时间: 2023-08-24 17:07:34 浏览: 203
以下是用C语言解决日期计算的代码示例:
```c
#include <stdio.h>
// 判断是否为闰年
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// 计算月份和日期
void calculateDate(int year, int day, int *month, int *date) {
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) {
daysInMonth[1] = 29; // 闰年2月有29天
}
int i;
for (i = 0; i < 12; i++) {
if (day <= daysInMonth[i]) {
break;
}
day -= daysInMonth[i];
}
*month = i + 1;
*date = day;
}
int main() {
int year, day;
scanf("%d", &year);
scanf("%d", &day);
int month, date;
calculateDate(year, day, &month, &date);
printf("%d\n", month);
printf("%d\n", date);
return 0;
}
```
你可以将以上代码保存为一个名为 "date_calculation.c" 的文件,并使用C编译器进行编译和运行。然后按照题目要求输入年份和天数,程序将输出对应的月份和日期。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![text/x-c; charset=iso-8859-1](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)