C语言 编程实现: 从键盘输入年份和月份,计算从该年1月1日到当月月底的总天数。 输入格式: 输入某年某月,如:2021 11 输出格式: 从该年1月1日到当月月底的总天数,最后换行。 输入样例: 2021 11 输出样例: 334
时间: 2023-11-27 11:53:50 浏览: 421
以下是该问题的 C 语言代码实现:
```c
#include <stdio.h>
int main() {
int year, month;
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 每个月的天数
scanf("%d %d", &year, &month);
// 判断是否为闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days[1] = 29; // 闰年2月有29天
}
int total_days = 0;
for (int i = 0; i < month - 1; i++) {
total_days += days[i]; // 累加从1月到上一个月的天数
}
printf("%d\n", total_days + 1); // 加上当月天数并输出
return 0;
}
```
代码思路:
1. 定义每个月的天数数组 `days`,注意闰年的情况。
2. 从键盘输入年份和月份。
3. 根据输入的年份判断是否为闰年,并修改二月的天数。
4. 循环累加从1月到上一个月的天数。
5. 加上当前月的天数并输出。
相关问题
c语言编程实现: 从键盘输入年份和月份,计算从该年1月1日到当月月底的总天数。 输入格式: 输入某年某月,如:2021 11 输出格式: 从该年1月1日到当月月底的总天数,最后换行。 输入样例: 2021 11 输出样例: 334
以下是 C 语言实现的代码:
```c
#include <stdio.h>
#include <stdbool.h>
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year, month;
scanf("%d%d", &year, &month);
int days = 0;
for (int m = 1; m < month; m++) {
switch (m) {
case 2:
days += isLeapYear(year) ? 29 : 28;
break;
case 4:
case 6:
case 9:
case 11:
days += 30;
break;
default:
days += 31;
break;
}
}
days += 1; // 加上当月的第一天
printf("%d\n", days);
return 0;
}
```
首先通过 `scanf` 函数从标准输入中读取年份和月份。然后使用一个 `for` 循环,计算从该年1月1日到当月月底的总天数。循环中使用 `switch` 语句计算每个月的天数,需要特判闰年的二月。最后将当月的第一天加上去即可。
注意,这个程序假设输入的年份和月份都是合法的。如果需要对输入进行有效性检查,可以添加相应的代码。
用C语言写代码编程实现: 从键盘输入年份和月份,计算从该年1月1日到当月月底的总天数。 输入格式: 输入某年某月,如:2021 11 输出格式: 从该年1月1日到当月月底的总天数,最后换行。 输入样例: 2021 11 输出样例: 334
以下是用C语言实现的代码:
```c
#include <stdio.h>
int is_leap_year(int year) {
// 判断是否为闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
int main() {
int year, month;
int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int total_days = 0;
scanf("%d %d", &year, &month);
// 计算从 1 月到当前月份的总天数
for (int i = 0; i < month - 1; i++) {
total_days += days_in_month[i];
if (i == 1 && is_leap_year(year)) { // 如果是闰年且当前月份是 2 月
total_days++; // 2 月加一天
}
}
printf("%d\n", total_days);
return 0;
}
```
代码思路:
1. 首先定义了一个 `is_leap_year()` 函数,用于判断是否为闰年。
2. 定义 `days_in_month` 数组,存储每个月的天数。
3. 从键盘输入年份和月份。
4. 循环计算从 1 月到当前月份的总天数。如果当前月份是 2 月,还需要判断是否为闰年,如果是闰年则 2 月加一天。
5. 输出总天数。
注意:以上代码中,我们假设输入的年份和月份都是合法的,因此没有做输入验证。如果输入的年份或月份非法,程序可能会出现错误。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)