# Blue Bridge Cup: running exercise (java code)

Posted by smixcer on Sat, 19 Feb 2022 10:50:17 +0100

# Blue Bridge Cup: running exercise algorithm (java)

This article is only for reference, because the blogger is very delicious, so the method is very stupid. I hope the bosses can put forward valuable opinions!!! thank you!!!

## Original title

This question is a blank filling question. You only need to calculate the result and use the output statement in the code to output the filled result.

Xiao Lan exercises every day.

Under normal circumstances, Xiaolan runs 1km every day. If one day is Monday or the beginning of the month (1st), Xiaolan has to run 2km in order to motivate himself. If it's also Monday or the beginning of the month, Xiaolan also runs 2km.

Xiaolan has been running for a long time, from Saturday, January 1, 2000 (inclusive) to Thursday, October 1, 2020 (inclusive). How many kilometers does Xiaolan run during this period?

#### Operational limits

Maximum running time: 1s
Maximum operating memory: 128M

## Train of thought analysis

In this paper, the results of the algorithm can be divided into three parts:

• Part I: total mileage of Xiaolan running 1km every day (equivalent to total days)
• Part II: Xiaolan's total mileage of running one kilometer more at the beginning of each month
• Part III: total mileage of Xiaolan running 1 kilometer more every week (excluding the beginning of the month)
Therefore, the additional points needing attention in this question are:
When a day is both the beginning of the month and Monday, you can't add two more miles on that day!!!

Corresponding to the idea, the variables we need to solve are:

• You need to find the beginning of each month from January 1, 2000 to October 1, 2020;
• Need to find the Monday of each week from January 1, 2000 to October 1, 2020;
• The total number of days in these 20 years;
• You need to find the days that are both the beginning of the month and Monday;

Although it's badly written, it's still shameless to let it out...
The original answer without collation, without function, is very bloated, but it is written correctly,

```import java.util.Scanner;
import java.util.ArrayList;
// 1: No package required
// 2: Class name must be Main and cannot be modified

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

//The list collection is used to store the beginning of each month
ArrayList<Integer> list = new ArrayList<>();
int sum = 1;		//Record the total number of days. The initial value is 1, which means that January 1, 2000 has been included
int ans = 1;		//Total mileage, 1 means that you have run an extra kilometer on January 1, 2000 (at the beginning of the month)
//Part I: run an extra kilometer at the beginning of each month
for(int i = 2000; i <= 2020;i++){
//Add according to the number of days in each month
sum += 31;
ans++;		//Ran an extra kilometer
//Here is a brief judgment of leap years. It may not work well after the number of that year changes
if(i % 4 == 0){
sum += 29;
}else{
sum += 28;
}
ans++;
sum += 31;
ans++;
sum += 30;
ans++;
sum += 31;
ans++;
sum += 30;
ans++;
sum += 31;
ans++;
sum += 31;
ans++;
sum += 30;
ans++;
//As of October 1, 2020
if(i != 2020){
sum += 31;
ans++;
sum += 30;
ans++;
sum += 31;
ans++;
}
}
//Part II: total mileage of running 1km every day
ans += sum;
//Part III: run one kilometer more every week and eliminate the beginning of the month to prevent double counting.
for(int i = 3;i < sum;i += 7){
if(list.contains(i)){
continue;
}
ans++;
}
//Output results
System.out.println(ans);
scan.close();
}
}
```

Because the original answer is bad, the notes are not so detailed. You can see the sorted answer.

• Encapsulate the days of 12 months with an array;
• Write a function to judge leap year separately;
• The ArrayList set is used to store the beginning of each month;
• num represents the total number of days, and the initial value of 1 indicates that January 1, 2000 has been included;
• ans refers to the total mileage. The initial value is 1, which means that you have run an extra kilometer at the beginning of January 1, 2000;
• ans + + means running an extra kilometer at the beginning of the month or Monday.
```import java.util.Scanner;
import java.util.ArrayList;

public class Main{
public static void main(String[] args){
//Initialize the number of days in the month. The number of days in February will be processed additionally
int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//Create a new set, which is used to store all the days at the beginning of the month
ArrayList<Integer> list = new ArrayList<>();
int num = 1;	//Indicates the total number of days from January 1, 2000 to October 1, 2020. The initial value of 1 indicates that January 1, 2000 has been included.
int ans = 1;    //Indicates the total mileage, which is the result of the answer. An initial value of 1 means running an extra kilometer on January 1, 2000.

for(int i = 2000;i <= 2020;i++) {
monthDays[1] = 28;
//Judge whether the year is a leap year. If it is a leap year, February is 29 days
if(isLeapYear(i)){
monthDays[1] = 29;
}
for(int j = 0;j < monthDays.length;j++){
//If it has reached October 1, 2020, exit the cycle
if(i == 2020 && j == 9) break;
sum += monthDays[j];		//Total days
//At the beginning of the month, the total mileage is more than 1 kilometer;
ans++;
}
}

/*
At the end of the cycle, analyze the value of the variable:
list: list It includes all the days experienced at the beginning of the month from January 1, 2000 to October 1, 2020.
num: num The value of is the total number of days from January 1, 2020 (inclusive) to October 1, 2020 (inclusive).
ans: All extra mileage at the beginning of the month has been included.
*/

ans += sum;		//Run 1 kilometer a day

/**
* Calculate the extra kilometer run every week, and if it is the beginning of the month, there is no need to repeat the calculation.
*/
//Since the first day is Saturday, Monday starts from the third day.
for(int i = 3;i < sum;i += 7){
//If it is the beginning of the month, skip directly and do not double calculate.
if(list.contains(i)){
continue;
}
ans++;
}
}

/**
* Judge whether the year is a leap year
*/
public static boolean isLeapYear(int year){
return (year % 400 == 0 || year % 4 == 0 && year % 100 != 0);
}

}
```

Thank you for reading my thoughts hard!