跑步锻炼
小蓝每天都锻炼身体。正常情况下,小蓝每天跑 1 千米。如果某天是周一或者月初(1 日),为了
激励自己,小蓝要跑 2 千米。如果同时是周一或月初,小蓝也是跑 2 千米。小蓝跑步已经坚持了很长时间,从 2000 年 1 月 1 日周六(含)到 2020 年
10 月 1 日周四(含)。请问这段时间小蓝总共跑步多少千米?
代码如下:
public class Main { public static void main(String [] args) { int[] day1= {0,31,28,31,30,31,30,31,31,30,31,30,31}; int[] day2= {0,31,29,31,30,31,30,31,31,30,31,30,31}; int count=0; int week=6; for(int n=2000;n<=2020;n++) { if(n==2020) { for(int y=1;y<=9;y++) { for(int r=1;r<=day2[y];r++) { if(week==1||r==1) { count+=2; }else { count+=1; } week=(week+1)%7; } } }else if(n%4==0&&n%100!=0||n%400==0) { for(int y=1;y<=12;y++) { for(int r=1;r<=day2[y];r++) { if(week==1||r==1) { count+=2; }else { count+=1; } week=(week+1)%7; } } }else { for(int y=1;y<=12;y++) { for(int r=1;r<=day1[y];r++) { if(week==1||r==1) { count+=2; }else { count+=1; } week=(week+1)%7; } } } } System.out.println(count+2); } }
答案:8879