标签:style blog color io div 算法 amp log
知识点:
逻辑表达式的灵活运用
法1:
switch()的用法,注意字符用 case ‘a‘: ……;break; 数字用 case 6:……;break;
不要忘了break;
法2:
if……else 的用法
注意是否是闰年的算法
内容: 打印某年某月有几天
输入说明:
一行两个整数,前面是年份 后面是月份
输出说明:
一个整数
输入样例:
2009 6
输出样例 :
30
法1:
#include <stdio.h> int main() { int y,m; scanf("%d%d",&y,&m); if((y%4==0&&y%100!=0)||y%400==0) { switch (m) { case 1: printf("31\n");break; case 2: printf("29\n");break; case 3: printf("31\n");break; case 4: printf("30\n");break; case 5: printf("31\n");break; case 6: printf("30\n");break; case 7: printf("31\n");break; case 8: printf("31\n");break; case 9: printf("30\n");break; case 10: printf("31\n");break; case 11: printf("30\n");break; case 12: printf("31\n");break; } } else switch (m) { case 1: printf("31\n");break; case 2: printf("28\n");break; case 3: printf("31\n");break; case 4: printf("30\n");break; case 5: printf("31\n");break; case 6: printf("30\n");break; case 7: printf("31\n");break; case 8: printf("31\n");break; case 9: printf("30\n");break; case 10: printf("31\n");break; case 11: printf("30\n");break; case 12: printf("31\n");break; } return 0; }
法2:
#include <stdio.h> int main() { int y,m; scanf("%d%d",&y,&m); if (m==1||m==3||m==5||m==7||m==8||m==10||m==12) { printf("31\n"); } else { if (m==2) { if((y%4==0&&y%100!=0)||y%400==0) { printf("29\n"); } else { printf("28\n"); } } else { printf("30\n"); } } return 0; }
(c语法百题17)打印某年某月有几天,布布扣,bubuko.com
标签:style blog color io div 算法 amp log
原文地址:http://www.cnblogs.com/hlwyfeng/p/3889654.html