> For the complete documentation index, see [llms.txt](https://scls-cs.gitbook.io/scls-apcs-lab/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://scls-cs.gitbook.io/scls-apcs-lab/chang-jian-suan-fa-bei-song/week8.md).

# Week8

{% file src="/files/ydw56hehLHo5icUTEGha" %}

### 1.a: numberOfLeapYears

```java
public static int numberOfLeapYears(int year1, int year2)
{
    int leapYears = 0;
    for(int y = year1; y <= year2; y++)
        if(isLeapYear(y))
            leapYears++;
    return leapYears;
}
```

### 1.b: dayOfWeek

```java
public static int dayOfWeek(int month, int day, int year)
{
    int additionalDays = dayOfYear(month, day, year) - 1;
    return (firstDayOfYear(year) + additionalDays) % 7;
}
```

### 2.a: scoreGuess

```java
//第一种解法
public int scoreGuess(String guess)
{
    int count = 0;
    for(int i = 0; i < secret.length(); i++)
    {
        int j = i + guess.length();
        if(j <= secret.length() && secret.substring(i, j).equals(guess))
            count++;
    }
    return count * (guess.length() * guess.length());
}
```

<pre class="language-java"><code class="lang-java"><strong>//第二种解法
</strong><strong>public int scoreGuess(String guess)
</strong>{
    int count = 0;
    for(int i = 0; i &#x3C;= secret.length()-guess.length(); i++)
    {
        int j = i + guess.length();
        if(secret.substring(i, j).equals(guess))
            count++;
    }
    return count * (guess.length() * guess.length());
}
</code></pre>

<pre class="language-java"><code class="lang-java"><strong>//第三种解法：按照默写的思路(countSpaces)
</strong>public int scoreGuess(String guess)
{
    String x = secret;
    int count = 0;
    while(x.indexOf(guess)!=-1)){
        x = x.substring(x.indexOf(guess)+1);
        count++;
    }
    return count * (guess.length() * guess.length());
}
</code></pre>

### 2.b: findBetterGuess

```java
public String findBetterGuess(String guess1, String guess2)
{
    int score1 = scoreGuess(guess1);
    int score2 = scoreGuess(guess2);

    if(score1 > score2)
        return guess1;
    else if(score2 > score1)
        return guess2;
    else
    {
        if(guess1.compareTo(guess2) > 0)
            return guess1;
        else
            return guess2;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://scls-cs.gitbook.io/scls-apcs-lab/chang-jian-suan-fa-bei-song/week8.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
