【问题标题】:Sorting sides of n triangles according to the area根据面积对n个三角形的边进行排序
【发布时间】:2018-12-19 17:41:02
【问题描述】:

给定n个三角形,边为a,b,c,通过从最小到最大排序以相同的样式打印它们。 完整问题:https://www.hackerrank.com/challenges/small-triangles-large-triangles/problem

在解决方案中,我们有一个名为三角形的结构。它有3个整数a,b,c。制作了一个三角形数组,命名为 tr,并将输入传递给函数 sort_by_area。 我的方法是在这个数组上应用冒泡排序。但不是像我们在普通冒泡排序中那样比较 tr[j] > tr[j+1],而是比较 tr[j] 和 tr[j+ 1]。现在,如果 tr[j] > tr[j+1] 的面积:交换。

问题:最终结果是错误的。数组没有正确排序。一开始我以为是某个地方打错了,所以我重写了代码,但问题仍然存在。

double area (int a, int b, int c)
{
 double  p = (a+b+c)/2;
 return sqrt(p*(p-a)*(p-b)*(p-c));
}

void sort_by_area(triangle* tr, int n) {
/**
* Sort an array a of the length n
*/
int i,j;
double area1, area2;
triangle temp;
for(i = 0; i < n-1;++i)
{ 
    for(j = 0; j < n-i-1; ++j)
    {
        area1 = area(tr[j].a , tr[j].b, tr[j].c);
        area2 = area(tr[j+1].a , tr[j+1].b, tr[j+1].c);
        if(area1 > area2)
        {
            temp = tr[j];
            tr[j] = tr[j+1];
            tr[j+1] = temp;
        } 
    }
}

}

输入:20

23 37 47 22 18 5 58 31 31 28 36 40 54 62 11 31 41 14 53 18 54 41 38 55 55 44 44 44 48 18 26 41 65 20 23 21 58 61 50 28 56 56 20 39 32 33 45 49 26 41 62 31 46 39 48 49 67 57 33 45

预期输出: 22 18 5 31 41 14 20 23 21 54 62 11 26 41 65 58 31 31 20 39 32 26 41 62 44 48 18 23 37 47 53 18 54 28 36 40 31 46 39 33 45 49 57 33 45 28 56 56 41 38 55 55 44 44 48 49 67 58 61 50

实际输出: 22 18 5 54 62 11 31 41 14 20 23 21 26 41 65 20 39 32 58 31 31 26 41 62 23 37 47 44 48 18 53 18 54 28 36 40 31 46 39 33 45 49 57 33 45 28 56 56 41 38 55 55 44 44 48 49 67 58 61 50

【问题讨论】:

  • 您的索引在内部循环中是错误的。对于冒泡排序,您要针对i + 1n - 1 之间的每个元素检查元素i,但这里您要检查0 和n - i - 1 之间的每个元素。更改内部循环范围或将 i 添加到第二项的数组索引中。
  • 还要注意(a+b+c)/2整数除法,如果a+b+c 是奇数,则会计算不正确的区域;除以2.0 以确保浮点除法。
  • 不要计算面积(你不关心面积是多少,你只关心面积会变大还是变小)。请注意,对于正数,如果sqrt(a) &lt; sqrt(b) 为真,那么a &lt; b 也将为真(换句话说,您可以计算“面积平方”并改用它)。
  • @TypeIA 谢谢,但我还是不明白。我应该写 (j=i+1;j
  • @TypeIA 我实际上查了几个网站,他们都以类似的方式编写冒泡排序。以下是供参考的链接:www.geeksforgeeks.org/bubble-sort/ 和 www.programmingsimplified.com/c/source-code/c-program-bubble-sort 我不明白我的实现有什么问题,而且真的会如果您能举个例子,将不胜感激。谢谢。

标签: c bubble-sort


【解决方案1】:

这是我应对这一挑战的解决方案:

double area(triangle t){
    double p = (t.a+t.b+t.c)/2.0;
    return sqrt(p*(p-t.a)*(p-t.b)*(p-t.c));
}

void sort_by_area(triangle* tr, int n) {
    int i,j;
    for(i=0;i<n-1;i++){
        for(j=i+1;j<n;j++){
            if(area(tr[j]) < area(tr[i])){
                triangle temp = tr[i];
                tr[i]=tr[j];
                tr[j]=temp;
            }
        }
    }
}

你使用'for'循环来比较三角形是这样的(三角形,我的意思是三角形的面积):

for(i = 0; i < n - 1; ++i){
    ...
    ...
    for(j = 0; j < n - i - 1; ++j){
        ...
    }
}

仔细观察:您使用的循环无法比较数组的最后一个元素。

【讨论】:

  • 也许它会帮助更多地解释你是如何解决这个问题的,使用 cmets 来分享你的知识。设计有效的算法是思考和应用规则的结果。这些有助于得到一个好的答案。
  • 对不起,你是对的。我用我糟糕的英语尝试解释:)我希望这很有用:)
【解决方案2】:

我只是即兴创作了你的代码,它可以工作:

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

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

typedef struct triangle triangle;
float area(int a, int b, int c)
{
    float p;
    p = (float)(a + b + c) / 2;
    return sqrt(p * (p - a) * (p - b) * (p - c));
}
void sort_by_area(triangle* tr, int n)
{
    int i, j;

    float area1, area2;
    triangle temp[5];
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            area1 = area(tr[j].a, tr[j].b, tr[j].c);
            area2 = area(tr[j + 1].a, tr[j + 1].b, tr[j + 1].c);
            if (area1 > area2)
            {
                temp[0] = tr[j];
                tr[j] = tr[j + 1];
                tr[j + 1] = temp[0];
            }
        }
    }
}

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;
}

【讨论】:

  • 嗨。仅代码答案被视为低质量。你能添加一些文字来澄清吗? meta.stackoverflow.com/questions/300837/…
  • 我希望我可以添加文本,但我是 Stack Overflow 的新手,我不知道如何给出答案。甚至我也不得不花很多精力来编写这些代码。
  • @MadhavMishra 那么您应该阅读How to Ask 并继续回答...感谢您的努力!
  • 是的,当然。谢谢!!
【解决方案3】:

编辑@Zümrüd-ü Anka 的答案以匹配您在其他地方可以找到的冒泡排序。您的循环实际上应该可以正常工作,这里的两个 for 循环都与您的相同。这通过了 HackerRank 的测试用例。我认为实际上只是整数除法给你带来了问题。

double area(triangle t){
    double p = (t.a+t.b+t.c)/2.0;
    return sqrt(p*(p-t.a)*(p-t.b)*(p-t.c));
}

void sort_by_area(triangle* tr, int n) {
    int i,j;
    for(i=0;i<n-1;i++){
        for(j=0;j<n-i-1;j++){
            if(area(tr[j]) > area(tr[j+1])){
                triangle temp = tr[j+1];
                tr[j+1]=tr[j];
                tr[j]=temp;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2023-01-30
    • 2023-01-31
    • 2012-09-06
    • 2014-12-06
    • 2021-06-26
    • 2016-09-28
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多