【发布时间】:2012-09-17 04:57:14
【问题描述】:
我正在做这个编程挑战,可以在 www.interviewstreet.com 找到(它的第一个挑战值 30 分)。
当我提交解决方案时,我收到了一个结果,说答案是错误的,因为它只通过了 1/11 的测试用例。但是,我觉得已经测试了各种案例并且不明白我做错了什么。知道这些测试用例可能是什么会很有帮助,这样我就可以测试我的程序。
这是一个问题(在下面的灰线之间):
象限查询(30 分)
平面上有 N 个点。第 i 个点具有坐标 (xi, yi)。执行以下查询:
1) 反射点 i 和 j 之间的所有点,包括沿 X 轴。此查询表示为“X i j”
2) 反射点 i 和 j 之间的所有点,包括沿 Y 轴。此查询表示为“Y i j”
3) 计算点 i 和 j 之间有多少点,包括位于 4 个象限中的每一个。此查询表示为“C i j”
输入:
第一行包含 N,点数。接下来是 N 行。
第 i 行包含用空格分隔的 xi 和 yi。
下一行包含 Q 查询数。接下来的 Q 行每行包含一个查询,采用上述形式之一。
所有索引都是 1 索引。
输出:
为“C i j”类型的每个查询输出一行。对应的行包含 4 个整数;分别在第 1、2、3 和 4 象限中具有 [i..j] 范围内索引的点数。
约束:
1
1
您可以假设 X 或 Y 轴上没有任何点。
所有 (xi,yi) 都适合 32 位有符号整数
在所有查询中,1
示例输入:
4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
是 1 2
C 1 3
样本输出:
1 1 1 1
1 1 0 0
0 2 0 1
说明:
当查询说“X i j”时,这意味着将索引 i 和 j 之间的所有点都包括并反映沿 X 轴的那些点。这里的 i 和 j 与点的坐标无关。它们是指数。 i 指 i 点,j 指 j 点
“C 1 4”要求您“考虑索引在 {1,2,3,4} 中的点集。在这些点中,有多少分别位于第一、第二、第三和第四四边形? 答案显然是 1 1 1 1。
接下来,我们沿 X 轴反映索引“2 4”之间的点。所以新坐标是:
1 1
-1 -1
-1 1
1 1
现在“C 3 4”是“考虑在 {3,4} 中具有索引的点集。在这些点中,有多少分别位于第一、第二、第三和第四四边形?点 3 位于象限 2,点 4 位于象限 1。 所以答案是 1 1 0 0
我正在使用 PHP 进行编码,测试方法是使用 STDIN 和 STDOUT。
有什么想法可以用来测试我的代码吗?我不明白为什么我会失败 10 / 11 个测试用例。
另外,如果你有兴趣,这里是我的代码:
// The global variable that will be changed
$points = array();
/******** Functions ********/
// This function returns the number of points in each quadrant.
function C($beg, $end) {
// $quad_count is a local array and not global as this gets reset for every C operation
$quad_count = array("I" => 0, "II" => 0, "III" => 0, "IV" => 0);
for($i=$beg; $i<$end+1; $i++) {
$quad = checkquad($i);
$quad_count[$quad]++;
}
return $quad_count["I"]." ".$quad_count["II"]." ".$quad_count["III"]." ".$quad_count["IV"];
}
// Reflecting over the x-axis means taking the negative value of y for all given points
function X($beg, $end) {
global $points;
for($i=$beg; $i<$end+1; $i++) {
$points[$i]["y"] = -1*($points[$i]["y"]);
}
}
// Reflecting over the y-axis means taking the negative value of x for all given points
function Y($beg, $end) {
global $points;
for($i=$beg; $i<$end+1; $i++) {
$points[$i]["x"] = -1*($points[$i]["x"]);
}
}
// Determines which quadrant a given point is in
function checkquad($i) {
global $points;
$x = $points[$i]["x"];
$y = $points[$i]["y"];
if ($x > 0) {
if ($y > 0) {
return "I";
} else {
return "IV";
}
} else {
if ($y > 0) {
return "II";
} else {
return "III";
}
}
}
// First, retrieve the number of points that will be provided. Make sure to check constraints.
$no_points = intval(fgets(STDIN));
if ($no_points > 100000) {
fwrite(STDOUT, "The number of points cannot be greater than 100,000!\n");
exit;
}
// Remember the points are 1 indexed so begin key from 1. Store all provided points in array format.
for($i=1; $i<$no_points+1; $i++) {
global $points;
list($x, $y) = explode(" ",fgets(STDIN)); // Get the string returned from the command line and convert to an array
$points[$i]["x"] = intval($x);
$points[$i]["y"] = intval($y);
}
// Retrieve the number of operations that will be provied. Make sure to check constraints.
$no_operations = intval(fgets(STDIN));
if($no_operations > 100000) {
fwrite(STDOUT, "The number of operations cannot be greater than 100,000!\n");
exit;
}
// Retrieve the operations, determine the type and send to the appropriate functions. Make sure i <= j.
for($i=0; $i<$no_operations; $i++) {
$operation = explode(" ",fgets(STDIN));
$type = $operation[0];
if($operation[1] > $operation[2]) {
fwrite(STDOUT, "Point j must be further in the sequence than point i!\n");
exit;
}
switch ($type) {
case "C":
$output[$i] = C($operation[1], $operation[2]);
break;
case "X":
X($operation[1], $operation[2]);
break;
case "Y":
Y($operation[1], $operation[2]);
break;
default:
$output[$i] = "Sorry, but we do not recognize this operation. Please try again!";
}
}
// Print the output as a string
foreach($output as $line) {
fwrite(STDOUT, $line."\n");
}
更新:
我终于找到了一个我的程序失败的测试用例。现在我试图确定原因。这是关于大数测试的一个很好的教训。
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
12
C 1 10
X 1 3
C 5 5
是 2 10
C 10 10
C 1 10
X 1 3
C 5 5
是 2 10
C 10 10
X 3 7
C 9 9
我将通过初始化一个错误数组并确定哪些操作导致问题来正确测试这一点。
【问题讨论】:
-
您提供的问题链接返回错误。我想现在没有人能够回答。如果您有副本,请将其粘贴到您的问题中。这就是为什么我们总是建议不要依赖外部链接...
-
好的,感谢您指出这一点。解决它。希望对您有所帮助并且仍然可读。
-
你可能想稍微缩短一下标题。
-
您能否将标题更改为与正在解决的问题更相关的我。你也可以清理格式吗?
-
这样更清楚吗?哪些区域需要格式化?