这是题目内容 以下是本人代码 #include<stdio.h> int main() while(scanf(“%d%d%d”,&year,&month,&day)!=EOF) if(month>12) } return 0; |
|
数组越界了吧
|
|
可我没发现有哪个数组越界啊,还望指点一下,谢谢 |
|
20分 |
提示:年月日均不可小于等于0,但是输入的测试数据有可能出现小于0的情况哦。
|
10分 |
比如:
2015 -1 0 |
10分 |
奔跑吧,参考:
#include <stdio.h> #include <stdlib.h> int year_day[2][13] = { { 0, 31, 28, 31, 30 ,31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30 ,31, 30, 31, 31, 30, 31, 30, 31 }, //闰年 }; int check(int year, int month, int day) { int pos = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); if (year > 0 && month > 0 && month <= 12 && day > 0 && day <= year_day[pos][month]) return 1; return 0; } int main(void) { int year, month, day; int i, days, pos; while(3 == scanf("%d%d%d", &year, &month, &day)) { if (0 == check(year, month, day)) { printf("Illegal date\n"); continue; } days = 0; pos = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); for (i = 1; i < month; i++) days += year_day[pos][i]; days += day; printf("%d\n", days); } return 0; } |
恩恩,我题目理解错误,谢谢 |