2201 - 计算天数(case语句)
输入年、月,输出该月的天数。
Input
输入年、月
Output
输入天数
Examples
Input
1298 2
Output
28
Hint
注意闰年、平年的区别!
Solution C
#include <stdio.h> #include <stdlib.h> int main() { int y,m,a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; scanf("%d%d",&y,&m); if(m!=2) printf("%d",a[m]); if(m==2) if(y%4==0 || y%400==0) printf("29"); else printf("28"); return 0; }
Solution C++
#include <iostream> #include <cstdio> using namespace std; int main() { int year,month; cin>>year>>month; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: cout<<31<<endl; break; case 4: case 6: case 9: case 11: cout<<30<<endl; break; default: //2月 if( (year%100 != 0 && year%4 == 0) || (year%400 == 0) ) { //如果是闰年,2月29天 cout<<29<<endl; } else { //如果是平年,2月28天 cout<<28<<endl; } break; } return 0; }
Hint
注意闰年、平年的区别!