闰年的计算方法:
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 }