第十一届蓝桥杯省赛JavaC组第三题跑步锻练
题目
package Work11;
public class T03 {
/*
* 先设定一个类判断是否为闰年 闰年可以被4整除,但不能被100整数 ,或 可以被400整除
* 再设定一个日期数组,存放每个月对应的天数
* 设定一个类,返回类型为int,返回数值为该月对应的天数
* 最后输出结果要加2,因为整套代码下来,你会发现没算10月1日那一天的
*/
static int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main(String[] args) {
// sum用于计算这天是否为星期1
int sum = 0;
// 结果
int res = 0;
/*
* weekday用于判断当天是否为星期一,
* 因为2020年1月1日为星期六所以sum初始化为0,取余7等于0代表这天为星期一
*/
for (int i = 2000; i < 2020; i++) {//2000年循环到2019年
for (int j = 1; j < 13; j++) {//1月循环到12月
for (int k = 1; k <= getDays(i, j); k++) {//该月i对应的天数
int weekday = (sum+5)%7;
if (k==1 || weekday==0) {//如果为每月一号和星期一跑两千米所以res+=2
res+=2;
}else {//否则+1
res++;
}
// 每过一天,天数加一
sum++;//1,2,3,4,5,6,7,8......
}
}
}
// 2020年1月到10月跑的
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= getDays(2020,i); j++) {
int weekday = (sum+5)%7;
if (j==1 || weekday==0) {
res+=2;
}else {
res++;
}
sum++;
}
}
System.out.println(sum);
System.out.println(res+2);
}
// 判断闰年,闰年返回true
public static boolean isRunNian(int year) {
return (year % 400==0 || year%4==0 && year%100!=0);
}
// 返回该月天数
public static int getDays(int year,int month) {
if(month==2 && isRunNian(year)) {//如果月份为2且这年为闰年
return days[month]+1;//返回二月对应的天数并且加一
}else {
return days[month];//返回天数
}
}
}