Week8
Last updated
Last updated
public static int numberOfLeapYears(int year1, int year2)
{
int leapYears = 0;
for(int y = year1; y <= year2; y++)
if(isLeapYear(y))
leapYears++;
return leapYears;
}
public static int dayOfWeek(int month, int day, int year)
{
int additionalDays = dayOfYear(month, day, year) - 1;
return (firstDayOfYear(year) + additionalDays) % 7;
}
//第一种解法
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());
}
//第二种解法
public int scoreGuess(String guess)
{
int count = 0;
for(int i = 0; i <= secret.length()-guess.length(); i++)
{
int j = i + guess.length();
if(secret.substring(i, j).equals(guess))
count++;
}
return count * (guess.length() * guess.length());
}
//第三种解法:按照默写的思路(countSpaces)
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());
}
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;
}
}