AP计算机2019年考试:主观题解答
第一大题
第一问
考察标准for循环的使用和if条件判断,简单的累加计数。
public static int numberOfLeapYears(int year1, int year2)
{
int count = 0;
for (int y = year1; y <= year2; y++)
{
if (isLeapYear(y))
{
count++;
}
}
return count;
}
第二问
需要正确使用题目中给定的两个method,考察取模运算%。
public static int dayOfWeek(int month, int day, int year)
{
int startDay = firstDayOfYear(year);
int nthDay = dayOfYear(month, day, year);
int returnDay = (startDay + nthDay - 1) % 7;
return returnDay;
}
第二大题
送分题。需要正确实现活跃天数的逻辑,另一个重点是求平均数以及int与double除法的区分。
public class StepTracker
{
private int minSteps;
private int totalSteps;
private int numDays;
private int numActiveDays;
public StepTracker(int threshold)
{
minSteps = threshold;
totalSteps = 0;
numDays = 0;
numActiveDays = 0;
}
public void addDailySteps(int steps)
{
totalSteps += steps;
numDays++;
if (steps >= minSteps)
{
numActiveDays++;
}
}
public int activeDays()
{
return numActiveDays;
}
public double averageSteps()
{
if (numDays == 0)
{
return 0.0;
}
else
{
return (double) totalSteps / numDays;
}
}
}
第三大题
第一问
需要掌握数组遍历,ArrayList基本操作以及判断String相等。
public ArrayList<String> getDelimitersList(String[] tokens)
{
ArrayList<String> d = new ArrayList<String>();
for (String str : tokens)
{
if (str.equals(openDel) || str.equals(closeDel))
{
d.add(str);
}
}
return d;
}
第二问
分隔符是否匹配,可以通过累加计数的比较来实现。注意后分隔符比前分隔符多就应立即确认不配对而结束循环,否则Example 2会被误判。
public boolean isBalanced(ArrayList<String> delimiters)
{
int openCount = 0;
int closeCount = 0;
for (String str : delimiters)
{
if (str.equals(openDel))
{
openCount++;
}
else
{
closeCount++;
}
if (closeCount > openCount)
{
return false;
}
}
if (openCount == closeCount)
{
return true;
}
else
{
return false;
}
}
第四大题
第一问
基本的二维数组遍历和随机数。
public LightBoard(int numRows, int numCols)
{
lights = new boolean[numRows][numCols];
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++)
{
double rnd = Math.random();
lights[r][c] = rnd < 0.4;
}
}
}
第二问
继续考察取模运算%以及对题目中给定条件的构造实现。
public boolean evaluateLight(int row, int col)
{
int numOn = 0;
for (int r = 0; r < lights.length; r++)
{
if (lights[r][col])
{
numOn++;
}
}
if (lights[row][col] && numOn % 2 == 0)
{
return false;
}
if (!lights[row][col] && numOn % 3 == 0)
{
return true;
}
return lights[row][col];
}
1 条评论
AP计算机2019年考试:主观题 – AP计算机科学A · 2019年8月27日 下午5:29
[…] AP计算机2019年考试:主观题解答 […]