苏州做网站的公司有哪些模板下载网站
思路:①时间2000年1月1日到2020年10月一日
②每天跑1公里
③周一或者月初多跑1公里
使用一个循环,循环小蓝每天跑的日子+1;特殊的日子多+1;输出结果。
模拟两个不同的年份→正常年份+闰年 使用两个数组模拟
使用两个整型变量一个表示一周天数,一个表示跑步总公里数
使用循环依次判断不同年的不同天(考虑特殊年份第一层),若是周一或者一个月第一天则+2公里(考虑特殊日子第二层加判断)
代码实现👇
public static void main(String[] args) {int[] y1= {0,31,28,31,30,31,30,31,31,30,31,30,31};int[] y2= {0,31,29,31,30,31,30,31,31,30,31,30,31};int week=6;//初始从周六开始int result=2;//九月一号跑两公里for (int i = 2000; i <= 2020; i++) {if (i==2020) {for (int j = 1; j <= 9; j++) {//2020年九个月for (int d = 1; d <= y2[j]; d++) {if (week==1 || d==1) {result+=2;}else {result+=1;}week++;week%=7;}}}else if ((i%4==0&&i%100!=0) || i%400==0) {//闰年for (int j = 1; j <= 12; j++) {for (int d = 1; d <= y2[j]; d++) {if (week==1 || d==1) {result+=2;}else {result+=1;}week++;week%=7;}}}else {for (int j = 1; j <= 12; j++) {for (int d = 1; d <= y1[j]; d++) {if (week==1 || d==1) {result+=2;}else {result+=1;}week++;week%=7;}}}}System.out.println(result);}