初学C语言,还没有学数组,只能用switch case 的方法来做了。
思路:尽量让月份的数字能对应到24个case里面
PS:n代表年,y代表月,c代表日
#include<stdio.h>
int main()
{
int n,y,c;
scanf("%d/%02d/%02d",&n,&y,&c);
if(n%4==0&&n%100!=0||n%400==0)
{
y+=12;
}
else
{
y=y;
}
switch(y)
{
case 1:printf("%d",c);break;
case 2:printf("%d",31+c);break;
case 3:printf("%d",59+c);break;
case 4:printf("%d",90+c);break;
case 5:printf("%d",120+c);break;
case 6:printf("%d",151+c);break;
case 7:printf("%d",181+c);break;
case 8:printf("%d",212+c);break;
case 9:printf("%d",241+c);break;
case 10:printf("%d",273+c);break;
case 11:printf("%d",304+c);break;
case 12:printf("%d",334+c);break;
case 13:printf("%d",c);break;
case 14:printf("%d",31+c);break;
case 15:printf("%d",60+c);break;
case 16:printf("%d",91+c);break;
case 17:printf("%d",121+c);break;
case 18:printf("%d",152+c);break;
case 19:printf("%d",182+c);break;
case 20:printf("%d",213+c);break;
case 21:printf("%d",242+c);break;
case 22:printf("%d",274+c);break;
case 23:printf("%d",305+c);break;
case 24:printf("%d",335+c);break;
}
return 0;
}
有没有人可以分享一下,如何用数组,更简洁的写出来这个问题的解决方案的代码