今天这道题可以用俩种解法:
一:#include<stdio.h>
int main()
{
int y,m,d,n;
while (scanf("%d/%d/%d",&y,&m,&d)!=EOF)
{
if (m == 1)
n = d;
else if (m == 2)
n = 31 + d;
else if (m == 3)
n = 31 + 28 + d;
else if (m == 4)
n = 31 + 28 + 31 + d;
else if (m == 5)
n = 31 + 28 + 31 + 30 + d;
else if (m == 6)
n = 31 + 28 + 31 + 30 + 31 + d;
else if (m == 7)
n = 31 + 28 + 31 + 30 + 31 + 30 + d;
else if (m == 8)
n = 31 + 28 + 31 + 30 + 31 + 30 + 31 + d;
else if (m == 9)
n = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + d;
else if (m == 10)
n = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + d;
else if (m == 11)
n = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + d;
else if (m == 12)
n = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + d;
if ((y%4==0&&y%100!=0||y/400==0)&&m>=3)
n = n + 1;
printf("%d\n", n);
}
return 0;
}
第一种方法就是直接硬带,会受到题目时间的限制(应该)。
另一种就是条件语句的嵌套啥的
二:
#include<stdio.h>
int main()
{
int a, c, e;
while (scanf("%d/%d/%d", &a, &c, &e) != EOF)
{
if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0)
{
if (c >= 8)
{
printf("%d\n", e + 30 * (c - 1) + (c - 1) / 2);
}
else if (c == 1)
{
printf("%d\n", e);
}
else if (c == 2)
{
printf("%d\n", 31 + e);
}
else if (c == 3)
{
printf("%d\n", e + 60);
}
else if (4 <= c && c <= 7)
{
printf("%d\n", e + 30 * (c - 1) + (c - 2) / 2);
}
}
else
if (c >= 8)
{
printf("%d\n", e + 30 * (c - 1) + (c - 1) / 2 - 1);
}
else if (c == 1)
{
printf("%d\n", e);
}
else if (c == 2)
{
printf("%d\n", 31 + e);
}
else if (c == 3)
{
printf("%d\n", 59 + e);
}
else if (4 <= c && c <= 7)
{
printf("%d\n", e + 30 * (c - 1) + (c - 2) / 2 - 1);
}
}
return 0;
}
这俩种方法都是可以很轻松的得到答案。