小三角形、大三角形代码不起作用

问题描述 投票:0回答:2

所以,我一直在尝试编写 Hackerrank 中 C 语言的小三角形、大三角形问题的代码。之前,我陈述我面临的问题,我将附上问题-

我这里只写了sort_by_area、swap和area函数。其余的都是给定的,不可改变的。我编写的代码可以正确执行,但结构没有正确排序。这是预期的输出和我的输出-

我只是不明白为什么它会变得如此奇怪。如果有人能提供帮助,那就意义重大。

我的代码是-

#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 i, j, swapped;
    for (i = 0; i < n-1; i++)
   {
     swapped = 0;
     for (j = 0; j < n-i-1; j++)
     {
        if (area(tr[j].a, tr[j].b, tr[j].c) > area(tr[j+1].a, tr[j+1].b, tr[j+1].c))
        {
           swap(&tr[j], &tr[j+1]);
           swapped = 1;
        }
     }
     if (swapped == 0)
        break;
    }
}
void swap(struct triangle **xp, struct triangle **yp)
{
    struct triangle *temp = *xp;
    *xp = *yp;
    *yp = temp;
}
int area(int a, int b, int c){
       int p=(a+b+c)/2;
       int q=p*(p-a)*(p-b)*(p-c);
       return sqrt(q);
   }
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;
}```

arrays c sorting struct swap
2个回答
2
投票

启用所有警告

这很快导致

swap()
交换指针而不是数据。

// Corrected - swap data
void swap(struct triangle *xp, struct triangle *yp) {
  struct triangle temp = *xp;
  *xp = *yp;
  *yp = temp;
}

功能顺序

在调用

area()
swap()
定义之前先移动它们。


地区

(int) sqrt(q)
可能会为不同的
q
返回相同的值。

示例:

(int) sqrt(100)
(int) sqrt(110)
(int) sqrt(120)

全部返回10。排序不一定会按照地区排序。


简单返回面积的平方。从数学上讲,按面积平方排序,与面积相同。

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

虽然可以使用

double
进行编码,但让我们继续使用整数。

请注意

a+b+c
odd/2
形成截断商一样奇怪。

也许返回面积的平方,每边缩放 2?

int area_squared2(int a, int b, int c){
   a *= 2; b *= 2; c *= 2;
   // a+b+c is certianly even
   int p=(a+b+c)/2;
   int q=p*(p-a)*(p-b)*(p-c);
   return q;
}

剩下的一个问题是

int
溢出。考虑
long long
数学。

long long area_squared2LL(int a, int b, int c){
   long long aa = a * 2LL;
   long long bb = b * 2LL;
   long long cc = c * 2LL;
   long long pp = (aa+bb+cc)/2;
   long long qq = pp*(pp-aa)*(pp-bb)*(pp-cc);
   return qq;
}

提示:通过引用数据分配,而不是类型

更容易正确编码、审查和维护。

// triangle *tr = malloc(n * sizeof(triangle));
triangle *tr = malloc(sizeof *tr * n);
if (tr == NULL) {
  // use tr 
  ...
  free(tr);
  tr = NULL;
} 

0
投票
#include <stdio.h>
   #include <stdlib.h>
   #include <math.h>

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

  typedef struct triangle triangle;
  typedef struct triangle tr_temp;

  void sort_by_area(triangle* tr, int n)
 {
       int i, j, min;
       triangle tr_temp[100];
       double p[101], s[101], area[100], temp_area;
       for (i=0; i<n; i++)
      {
            p[i] = (tr[i].a + tr[i].b + tr[i].c)*(0.5);
            s[i] = sqrt(p[i]*(p[i]-tr[i].a)*(p[i]-tr[i].b)*(p[i]-tr[i].c));
            area[i] = s[i];    
      }
      //To check whether areas are calculated correctly or not
      /*for (i=0; i<n; i++)
       {
            printf("%d %d %d :- %lf\n", tr[i].a, tr[i].b, tr[i].c, 
            area[i]);
       }*/
      for (i=0; i<n; i++)
     {
          for (j=i+1; j<n; j++)
          {
                if (area[i] > area[j])
                {
                     temp_area = area[i];
                     area[i] = area[j];
                     area[j] = temp_area;

                     tr_temp[i].a = tr[i].a;
                     tr[i].a = tr[j].a;
                     tr[j].a = tr_temp[i].a;

                     tr_temp[i].b = tr[i].b;
                     tr[i].b = tr[j].b;
                     tr[j].b = tr_temp[i].b;

                     tr_temp[i].c = tr[i].c;
                     tr[i].c = tr[j].c;
                     tr[j].c = tr_temp[i].c;
                    /*tr_temp = tr[i];
                     temp_area = area[i];
              
                    tr[i] = tr[j];
                    area[i] = area[j];

                    tr[j] = tr_temp;
                    area[j] = temp_area;*/
                }
           }
        }
   }           


   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;
 }
© www.soinside.com 2019 - 2024. All rights reserved.