为什么我的代码适用于某些测试用例,但不适用于其他用例

时间:2018-07-14 08:38:24

标签: c

我被要求将此算法转换为代码

function OnCountryChange() {
    $.ajax({
        url: "/OnCountryChange",
        type: "POST",
        datatype: "json",
        headers: {
            "RequestVerificationToken": $('input[name = __RequestVerificationToken]').val()
        },
        data: {sCountryCode: "Test 123"}
    });
}

这是我的代码

//compute the max of size1 and (x_offset + size2).  Call this w

//compute the max of size1 and (y_offset + size2).  Call this h

//count from 0 to h. Call the number you count with y

//count from 0 to w. Call the number you count with x

  //check if  EITHER
  //    ((x is between x_offset  and x_offset +size2) AND 
  //     y is equal to either y_offset OR y_offset + size2 - 1 )
  //  OR
  //    ((y is between y_offset and y_offset + size2) AND
  //     x is equal to either x_offset OR x_offset + size2 -1)
  // if so, print a *

  //if not,
  // check if EITHER
  //    x is less than size1 AND (y is either 0 or size1-1)
  // OR
  //    y is less than size1 AND (x is either 0 or size1-1)
  //if so, print a #

  //else print a space
//when you finish counting x from 0 to w, 
//print a newline

但是,我的代码仅适用于某些测试用例 例如

#include <stdio.h>
#include <stdlib.h>

void squares(int size1, int x_offset, int y_offset, int size2) {
  int w = (size1 > x_offset + size2) ? size1 : x_offset + size2;
  int h = (size1 > y_offset + size2) ? size1 : y_offset + size2;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (((x >= x_offset && x < x_offset + size2) &&
           (x == y_offset || x == y_offset + size2 - 1)) ||
          ((y >= y_offset && y < y_offset + size2) &&
           (x == x_offset || x == x_offset + size2 - 1))) {
        printf("*");
      } else if ((x < size1 && (y == 0 || y == size1 - 1)) ||
                 (y < size1 && (x == 0 || x == size1 - 1))) {
        printf("#");
      } else {
        printf(" ");
      }
    }
printf("\n");
    }
}

有人可以帮我吗?非常感谢!

2 个答案:

答案 0 :(得分:1)

考虑以更易于遵循的方式对合同进行编码。

给予

  

//检查是否为
    //(((x在x_offset和x_offset + size2之间)AND
    // y等于y_offset或y_offset + size2-1-)
    // OR
    //((y在y_offset和y_offset + size2之间)AND
    // x等于x_offset或x_offset + size2 -1)
    //如果是,则打印*

如果代码不是

  if (((x >= x_offset && x < x_offset + size2) &&
       (x == y_offset || x == y_offset + size2 - 1)) ||
      ((y >= y_offset && y < y_offset + size2) &&
       (x == x_offset || x == x_offset + size2 - 1))) {
    printf("*");

  if (is_between(x, x_offset, x_offset + size2) && 
      is_either2(x, y_offset, y_offset + size2 - 1)) ||
      is_between(y, y_offset, y_offset + size2) && 
      is_either2(x, x_offset, x_offset + size2 - 1))) {
    printf("*");

发现错误可能更容易

// y is equal to either y_offset OR y_offset + size2 - 1 )  
//         v
is_either2(x, y_offset, y_offset + size2 - 1)

此外,由于我们只需要修改/更新功能/宏in_between() @Shawn,因此在之间的概念中,它也有助于处理包含性端点或排他性端点的合同歧义。

将非对称>=, <用于之间的 是可疑的。

答案 1 :(得分:0)

//a[ends-with(.,'War')]语句中检查条件。

Option Explicit Public Sub GetInfo() Dim d As WebDriver, Html As HTMLDocument Set d = New ChromeDriver Const URL = "https://en.wikipedia.org/wiki/War_correspondent" With d .Start "Chrome" .get URL Set Html = New HTMLDocument Html.body.innerHTML = .FindElementByXPath("//body").Attribute("innerHTML") Dim matchedStrings As Object, currentMatch As Long Set matchedStrings = .FindElementsByXPath("//a[starts-with(.,'War')]") If matchedStrings Is Nothing Then Debug.Print "No matches found" Exit Sub End If For currentMatch = 1 To matchedStrings.Count Debug.Print matchedStrings(currentMatch).Text Next currentMatch .Quit End With End Sub if

y is equal to either y_offset OR y_offset + size2 - 1第二行中的y == y_offset || y == y_offset + size2 - 1更改为(x == y_offset || x == y_offset + size2 - 1))

(y == y_offset || y == y_offset + size2 - 1))