c - 用 C 打印毕达哥拉斯三元组的表示

标签 c plot graph pythagorean

我正在尝试创建一个程序,用于在 C 中打印找到的毕达哥拉斯三元组的映射数据。到目前为止,我已经编写了能够找到三元组的程序。

#include <stdio.h>
#include <math.h>

int main (int argc, char * argv[]) {
    int a, b, c;
    int a2, b2, c2;
    int limit = 60;

    for (a = 1; a <= limit; a++) {
        a2 = a * a;
        for (b = 1; b <= limit; b++) {
            b2 = b * b;
            for (c = 0; c <= ((int)sqrt(a2+b2)); c++) {
                c2 = c * c;
                if (a < b && (c2 == (a2 + b2))) {
                    printf("triple: %d %d %d\n", a, b, c);
                }
            }
        }
    }
}

预期的输出格式为:

123456789012345678901234567890123456789012345678901234567890
1\
2 \
3  \
4  *\
5    \
6     \
7      \
8     * \
9        \
0         \
1          \
2    *   *  \

我正在尝试编写一个执行此操作的循环,但想不出如何以这种方式打印。有什么建议吗?

更新:我设法打印了 x 轴和 y 轴(x = a 和 y = b)。值是正确的,现在剩下映射部分。

    for (int x = 0; x < a; x++) { // x-axis = a
        printf("%d ", x);
    }

    printf("\n");

    for (int y = 0; y < b; y++) { // y-axis = b

        printf("%d\n", y);
    }

更新:修改了代码,输出正在打印,但打印空格有问题。尝试手动向“\”和“*”添加空格,但这只会扭曲整个图像。

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool is_perfect_square(int num);

int main (int argc, char * argv[]) {

    int a, b, c;
    int a2, b2, c2;
    int limit = 60;
    bool flag = false;

    for (a = 1; a <= limit; a++) {
        a2 = a * a;
        for (b = 1; b <= limit; b++) {
            b2 = b * b;
            for (c = 0; c <= ((int)sqrt(a2+b2)); c++) {
                c2 = c * c;
                if (a < b && (c2 == (a2 + b2))) {
                    // printf("triple: %d %d %d\n", a, b, c);
                }
            }
        }
    }

    for (int x = 0; x < a; x++) {
        for (int y = 0; y < b; y++) {
            if (x == 0) {
                printf("%d ", ((y+1)% 10));
            } else if (y == 0) {
                printf("%d ", (x % 10));
            } else if (x == y) {
                printf("\\");
            } else if (is_perfect_square((x*x) + (y*y))) {
                printf("*");
            }
        }
        printf("\n");
    }
}

bool is_perfect_square (int num) {
    int root = (int)(sqrt(num));

    if (num == root * root) {
        return true;
    } else {
        return false;
    }
}

仍在研究可能的解决方案。

最佳答案

提示:

有一个索引为 i,j 的嵌套循环;

for i 0.. limit {
  for j 0 .. limit {
   /*Implement the below algorithm here!!*/
  }
    printf("\n")
}

循环内使用的算法:

  • 如果 i == 0 通过打印 (j+1)% 10 来打印 x 轴值 [见末尾的注释]
  • else if j == 0 通过打印 i % 10 打印 y 轴值
  • 否则如果 i == j 打印 '\'
  • 否则如果 is_perfect_square((i*i) + (j*j)) 返回 1,打印 '*'
  • 否则打印一个空格。

is_perfect_square 函数的规范:如果输入是一个完美的正方形,则该函数返回 1,否则返回 0。例如:

  • is_perfect_square(25) 应该返回 1
  • is_perfect_square(7) 应该返回 0
  • is_perfect_square(49) 应该返回 1

注意:i == 0 案例应该打印 j%10 以 0 开始输出以表示原点。但是所提供的输出以 1 开头。因此使用 (j+1)%10

您可能需要处理一些特殊情况,一旦在代码中实现了该算法,这些情况就应该很简单了。

关于c - 用 C 打印毕达哥拉斯三元组的表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35277957/

相关文章:

类型转换 void 指针(在结构中工作时)

代码块中的 C 编程出现 undefined reference 'WinMain' 的错误

r - 如何使用 ggplot2 给图形编号

python - 是否有与 Python matplotlib 的 tiny_layout() 等效的 MatLab?

graph - Cypher 查询中排名前 10 的 HashTags

algorithm - 什么是父节点以及如何存储它?

c - 修改函数中的指针值

c - 如何控制变量中的单个位?

python - 如何在 Python 中创建三元等高线图?

c++ - Boost 捆绑属性 : Do I need to initialize all properties for each edge?