0

我正在尝试使用C lang编写一个程序来计算给定的两个数字A和B,从A到B有多少个数字可以被另一个数字K整除。例如,如果A是1,B是10 ,而 K 为 3,则有 3 个数满足此条件:3、6 和 9。

当试图在在线编译器中调试它时,我得到了这个错误:

Program received signal SIGFPE, Arithmetic exception.
0x00005555555553fd in main () at main.c:346
346             int temp = j % newArr[j][2];

模有什么问题吗?

这是我目前的尝试。我已经试过了js,它可以工作,但不能C

int main(void)
{
  int arr[] = {100,16,9905,8,7346,...,301n]
  //the input is array 0f 301 integers, which the 
     first element is just the number of test case

  int newArr[100][3];
  int no = 1;
  int result = 0;
  int x, y, i, j;
  int start, end;

  for (x = 0; x < 100; x++)
  {
    for (y = 0; y < 3; y++)
    {
      newArr[x][y] = arr[no];
      no += 1;
    }
  }

  for (i = 0; i < 100; i++)
  {
    if (newArr[i][0] < newArr[i][1])
    {
      start = newArr[i][0];
      end = newArr[i][1];
    }
    else
    {
      start = newArr[i][1];
      end = newArr[i][0];
    }

    for (j = start; j <= end; j++)
    {
      int temp = j % newArr[j][2];

      if ( temp == 0)
      {
        result += 1;
      }

      printf(" %d result %d\n",i, result);
    }

    printf("case %d : %d \n", i, result);
    result = 0;
  }

  return 0;
}

提前致谢!

4

1 回答 1

0

这里没有技术问题:你混淆了ij

int temp = j % newArr[j][2];

应该成为

int temp = j % newArr[i][2];

于 2021-11-16T15:08:07.267 回答