如何使我的代码适用于更大的输入

发布于 2025-01-12 06:53:44 字数 2184 浏览 4 评论 0原文

我正在尝试解决黑客排名问题“小三角形,大三角形” 我必须通过增加面积顺序来排列三角形 我编写的代码在我给出的较小输入上工作正常,但在较大输入上失败,在接受输入后我刚刚被终止 例如, 处理输入 3 7 24 25
5 12 13
3 4 5

不工作时 10
67 67 19
3 57 55
33 33 49
61 58 59
23 43 35
48 42 45
23 12 27
41 34 22
26 49 35
63 46 45
无法理解为什么!谢谢你!!提前

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

struct triangle
{
    int a;
    int b;
    int c;
};

typedef struct triangle triangle;
void sort_by_area(triangle *tr, int n)
{
    int *p = (int*)malloc(n * sizeof(int));
    int *middle = (int*)malloc((3*n)*sizeof(int));
    // doing sum of side of triangle
    for (int i = 0; i < n; i++)
    {
        p[i] = tr[i].a + tr[i].b + tr[i].c;
    }
    // arranging sum in increasing order
    // as more the sum of a+b+c more its area in herons formula
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i] < p[j])
            {
                int temp;
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
    int num = 0;
    // storing sides in increasing order in middle
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i] == tr[j].a + tr[j].b + tr[j].c)
            {
                middle[num] = tr[j].a;
                num++;
                middle[num] = tr[j].b;
                num++;
                middle[num] = tr[j].c;
                num++;
            }
        }
    }
    num=0;
    // copying increassed order in tr poiinter in question
    for (int i = 0; i < n; i++)
    {
        tr[i].a = middle[num];
        num++;
        tr[i].b = middle[num];
        num++;
        tr[i].c = middle[num];
        num++;
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    triangle *tr = malloc(n * sizeof(triangle));
    for (int i = 0; i < n; i++) {
        scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
    }
    sort_by_area(tr, n);
    for (int i = 0; i < n; i++) {
        printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
    }
    return 0;
}

I'm trying to solve the hacker rank question "Small triangle, Large triangle"
I have to arrange triangle by increasing order of area
the code I wrote is working fine on smaller input given by me but fails on the larger inputs, after taking inputs I just get terminated
eg,
working on input
3
7 24 25
5 12 13
3 4 5

not working when
10
67 67 19
3 57 55
33 33 49
61 58 59
23 43 35
48 42 45
23 12 27
41 34 22
26 49 35
63 46 45
unable to understand why!! Thank YOU!! in advance

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

struct triangle
{
    int a;
    int b;
    int c;
};

typedef struct triangle triangle;
void sort_by_area(triangle *tr, int n)
{
    int *p = (int*)malloc(n * sizeof(int));
    int *middle = (int*)malloc((3*n)*sizeof(int));
    // doing sum of side of triangle
    for (int i = 0; i < n; i++)
    {
        p[i] = tr[i].a + tr[i].b + tr[i].c;
    }
    // arranging sum in increasing order
    // as more the sum of a+b+c more its area in herons formula
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i] < p[j])
            {
                int temp;
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
    int num = 0;
    // storing sides in increasing order in middle
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i] == tr[j].a + tr[j].b + tr[j].c)
            {
                middle[num] = tr[j].a;
                num++;
                middle[num] = tr[j].b;
                num++;
                middle[num] = tr[j].c;
                num++;
            }
        }
    }
    num=0;
    // copying increassed order in tr poiinter in question
    for (int i = 0; i < n; i++)
    {
        tr[i].a = middle[num];
        num++;
        tr[i].b = middle[num];
        num++;
        tr[i].c = middle[num];
        num++;
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    triangle *tr = malloc(n * sizeof(triangle));
    for (int i = 0; i < n; i++) {
        scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
    }
    sort_by_area(tr, n);
    for (int i = 0; i < n; i++) {
        printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2025-01-19 06:53:45

有点奇怪,你的函数被称为 sort_by_area 但它似乎没有计算任何面积。相反,它似乎计算三角形的周长。

无论如何……这里有一个错误。 (注意:可能还有其他错误,但首先您需要修复此问题

int *middle = (int*)malloc((3*n)*sizeof(int));

...
...

int num = 0;
// storing sides in increasing order in middle
for (int i = 0; i < n; i++)
{
    for (int j = 0; j < n; j++)
    {
        if (p[i] == tr[j].a + tr[j].b + tr[j].c)
        {
            middle[num] = tr[j].a;
            num++;
            middle[num] = tr[j].b;
            num++;
            middle[num] = tr[j].c;
            num++;
        }
    }
}

您有两个从 0 到 n 的嵌套循环。当 if 语句为 true 时,您将 num 增加 3 次。

因此,如果 p[i] == tr[j].a + tr[j].b + tr[j].c 始终为 true(这是可能的),则最终会得到 < code>num 等于3*n*n

您使用 num 作为 middle 的索引,但 middle 仅分配用于保存 3*n 整数。

所以你是在分配的区域之外写的。这是未定义的行为。

顺便说一句

在 C 中对数组进行排序时,您几乎应该始终使用 qsort

It's kind of strange that your function is called sort_by_area but it doesn't seem to calculate any area. Instead it seems to calculate the perimeter of the triangles.

Anyway... there is a bug here. (note: There may be other bugs but to start with you need to fix this)

int *middle = (int*)malloc((3*n)*sizeof(int));

...
...

int num = 0;
// storing sides in increasing order in middle
for (int i = 0; i < n; i++)
{
    for (int j = 0; j < n; j++)
    {
        if (p[i] == tr[j].a + tr[j].b + tr[j].c)
        {
            middle[num] = tr[j].a;
            num++;
            middle[num] = tr[j].b;
            num++;
            middle[num] = tr[j].c;
            num++;
        }
    }
}

You have two nested loops going from 0 to n. When the if statement is true, you increment num 3 times.

So in case p[i] == tr[j].a + tr[j].b + tr[j].c is always true (which is possible), you end up with num being equal to 3*n*n.

And you use num as index into middle but middle is only allocated to hold 3*n integers.

So you are writing outside the allocated area. That's undefined behavior.

BTW

When sorting arrays in C you should nearly always use qsort

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文