小编Ros*_*ine的帖子

如何计算可以组成的三角形数量

下面的代码应该计算出给定范围 1...N 中 3 个不同整数的每个三元组可以形成的三角形的数量。然而,当我输入 5 时,它给出的是 34,而正确答案是 3:唯一可能的三角形是 (2, 3, 4)、(2, 4, 5) 和 (3, 4, 5)。

 // C code to count the number of possible triangles using

  #include <stdio.h>
  int main()
  {   int N, count=0;
      setvbuf(stdout, NULL, _IONBF, 0);
      printf("Please input the value of N: \n");
      scanf("%d", &N );
      for (int i = 1; i < N; i++) {
          for (int j = 1; j < N; j++) {
              // The innermost loop checks for the triangle
              // …
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
538
查看次数

标签 统计

c ×1