我被要求把这个算法翻译成代码

//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");
    }
}

但是,我的代码只适用于一些测试用例
例如
Testing ./squares 1 0 0 1
PASSED
 - Correct
Testing ./squares 1 0 0 5
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 0 9
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 1 1
PASSED
 - Correct
Testing ./squares 1 0 1 5
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 1 9
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 3 1
PASSED
 - Correct
Testing ./squares 1 0 3 5
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 3 9
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 8 1
PASSED
 - Correct

有人能帮我吗?太感谢你了!

最佳答案

考虑以一种更容易遵循的方式对合同进行编码。
鉴于
//检查是否有
//((x在x_offset和x_offset+size2之间)和
//y等于y_偏移量或y_偏移量+大小2-1)
//或者
//((y在y_offset和y_offset+size2之间)和
//x等于x_偏移量或x_偏移量+大小2-1)
//如果是,请打印a*
如果代码不是

  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
在中间使用非对称>=, <是可疑的。

关于c - 为什么我的代码适用于某些测试用例,但不适用于其他用例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51336773/

10-13 08:24