qq_34015982 于 2016.02.25 09:09 提问

3个回答

caozhy      2016.02.25 21:29
`````` #include <stdio.h>

int mt[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

void set2(int y)
{
if (y % 4 != 0)
{
mt[2] = 28;
}
else
{
if (y % 100 == 0 && y % 400 != 0)
mt[2] = 28;
else
mt[2] = 29;
}
}

{
printf("1. y/n/d -> yd\n");
printf("2. yd -> y/n/d\n");
printf("3. exit\n");
}

int ymd2yd(int y, int m, int d)
{
set2(y);
int r = 0;
for (int i = 0; i < m; i++)
r += mt[i];
r += d;
return r;
}

void yd2ymd(int yd, int * r)
{
set2(r[0]);
int m = 0;
while (yd > mt[m])
{
yd -= mt[m++];
}
r[1] = m;
r[2] = yd;
}

int main()
{
char c;
while (scanf("%c", &c), 1)
{
switch (c)
{
case '1':
int y, int m, int d;
scanf("%d", &y);
scanf("%d", &m);
scanf("%d", &d);
printf("%d\n", ymd2yd(y, m, d));
break;
case '2':
int yd;
int r[3];
scanf("%d", &r[0]);
scanf("%d", &yd);
yd2ymd(yd, r);
printf("%d %d %d\n", r[0], r[1], r[2]);
break;
case '\n':
break;
default:
return 0;
}
}
return 0;
}

``````
caozhy      2016.02.25 21:33
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 1 2016 3 1 61
4. y/n/d -> yd
5. yd -> y/n/d
6. exit 1 2016 7 18 200
7. y/n/d -> yd
8. yd -> y/n/d
9. exit 1 2015 12 31 365
10. y/n/d -> yd
11. yd -> y/n/d
12. exit 2 2016 61 2016 3 1
13. y/n/d -> yd
14. yd -> y/n/d
15. exit 2 2015 61 2015 3 2
16. y/n/d -> yd
17. yd -> y/n/d
18. exit 2 2015 225 2015 8 13
19. y/n/d -> yd
20. yd -> y/n/d
21. exit 3 Press any key to continue
caozhy      2016.02.25 21:33
`````` 1. y/n/d -> yd
2. yd -> y/n/d
3. exit 1
2016 3 1
61
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 1
2016 7 18
200
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 1
2015 12 31
365
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 2
2016 61
2016 3 1
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 2
2015 61
2015 3 2
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 2
2015 225
2015 8 13
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 3
Press any key to continue
``````