C语言程序设计:输入某年某月输出某月有多少天?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言程序设计:输入某年某月输出某月有多少天?相关的知识,希望对你有一定的参考价值。
#include <stdio.h>void main()
int year ,month;
int month_day(int year,int month);
printf("输入年份:");
scanf("%d",&year);
printf("输入月份:");
scanf("%d",&month);
printf("%d年%d月有%d天",year,month,month_day(year,month));
int month_day(int year,int month)
int day;
switch(month)
int day;
case 1:
case 3:
case 5:
case 7:
case 8:
case10:
case 12:
day = 31;
break;
case 2:
if(year%100!=0&&year%4==0)
day = 29;
else
day = 28;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
default:
printf("输入月份不存在");
return day;
参考技术A #include <stdio.h>
int main ()
int year,month,day;
day=0;
printf("请输入年份和月份:\n");
scanf("%d %d",&year,&month);
switch(month)
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
day=31; break;
case 4 :
case 6 :
case 9 :
case 11 :
day=30; break;
case 2 :
if(year%4==0&&year%100!=0 || year%400==0)
day=29;
else day=28; break;
default:
printf("Date error!"); break;
printf("day=%d\n",day);
return 0;
参考技术B #include
<stdio.h>
void
main()
int
year
,month;
int
month_day(int
year,int
month);
printf("输入年份:");
scanf("%d",&year);
printf("输入月份:");
scanf("%d",&month);
printf("%d年%d月有%d天",year,month,month_day(year,month));
int
month_day(int
year,int
month)
int
day;
switch(month)
int
day;
case
1:
case
3:
case
5:
case
7:
case
8:
case10:
case
12:
day
=
31;
break;
case
2:
if(year%100!=0&&year%4==0)
day
=
29;
else
day
=
28;
break;
case
4:
case
6:
case
9:
case
11:
day
=
30;
break;
default:
printf("输入月份不存在");
return
day;
参考技术C 用数组和条件语句做
1097某年某月天数
描述
打印某年某月有多少天。
输入
输入一行,包含2个整数 分别代表年和月
输出
输出一行,包含1个整数,表示该年的这个月份一共有多少天.
输入样例 1
2008 2
输出样例 1
29
提示
闰年的计算方法:
1.年数能被4整除,并且不能被100整除;
2.能被400整除的整数年份。
错误示范:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int main() 5 { 6 int a; 7 int x; 8 cin>>a>>x; 9 if(a%4==0&&a%100!=0||a%400==0) 10 { 11 if(x==2) cout<<"29"; 12 } 13 else 14 { 15 switch(x) 16 { 17 case 1: 18 cout<<"31"; 19 break; 20 case 2: 21 cout<<"29"; break; 22 case 3: 23 cout<<"31"; break; 24 case 4: 25 cout<<"30"; break; 26 case 5: 27 cout<<"31"; break; 28 case 6: 29 cout<<"30"; break; 30 case 7: 31 cout<<"31"; break; 32 case 8: 33 cout<<"31"; break; 34 case 9: 35 cout<<"30"; break; 36 case 10: 37 cout<<"31"; break; 38 case 11: 39 cout<<"30"; break; 40 case 12: 41 cout<<"31"; break; 42 } 43 } 44 return 0; 45 }
这题不难,但是对闰年的考虑加大了难度。
考虑到二月二十九号的情况题目就变得简单了。
正确代码:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int main() 5 { 6 int a; 7 int x; 8 cin>>a>>x; 9 if(a%4==0&&a%100!=0||a%400==0) 10 { 11 if(x==2) cout<<"29"; 12 } 13 else 14 { 15 switch(x) 16 { 17 case 1: 18 cout<<"31"; 19 break; 20 case 2: 21 cout<<"28"; break; 22 case 3: 23 cout<<"31"; break; 24 case 4: 25 cout<<"30"; break; 26 case 5: 27 cout<<"31"; break; 28 case 6: 29 cout<<"30"; break; 30 case 7: 31 cout<<"31"; break; 32 case 8: 33 cout<<"31"; break; 34 case 9: 35 cout<<"30"; break; 36 case 10: 37 cout<<"31"; break; 38 case 11: 39 cout<<"30"; break; 40 case 12: 41 cout<<"31"; break; 42 } 43 } 44 return 0; 45 }
以上是关于C语言程序设计:输入某年某月输出某月有多少天?的主要内容,如果未能解决你的问题,请参考以下文章
输入某年某月,判断该月有多少天,并判断是不是为闰年,谁会编这个程序