- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试用 C 语言解决 HackerRank 网站中的“小三角形,大三角形”问题,这是一种排序数组问题,我使用了选择排序算法。我的代码似乎可以工作,但它仅适用于某些测试用例。这是任务:
You are given triangles, specifically, their sides a, b and c. Print them in the same style but sorted by their areas from the smallest one to the largest one. It is guaranteed that all the areas are different.
The best way to calculate a volume of the triangle with sides a,b and c is Heron's formula.
这是我的代码:
#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) {
//Sort an array a of the length n
int area[n];
int p[n];
int temp[3];
int min;
double doubledArea[n];
//calculate the volume
for(int i = 0; i < n; i++)
{
// volume = 0.5 * (H * W * L)
p[i] = (tr[i].a + tr[i].b + tr[i].c) * 0.5;
doubledArea[i] = p[i]*(p[i]-tr[i].a)*(p[i]-tr[i].b)*(p[i]-tr[i].c);
area[i] = sqrt(doubledArea[i]);
}
//sort by volume
for(int i = 0; i < n; i++)
{
min = i;
for (int j = i+1; j < n; j++)
if(area[j] < area[min])
{
min = j;
temp[0] = tr[i].a;
temp[1] = tr[i].b;
temp[2] = tr[i].c;
tr[i].a = tr[min].a;
tr[i].b = tr[min].b;
tr[i].c = tr[min].c;
tr[min].a = temp[0];
tr[min].b = temp[1];
tr[min].c = temp[2];
}
}
}
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;
}
当我尝试举例时:
3
9 8 9
4 5 7
2 5 4
它可以工作,但不适用于以下输入:
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
最佳答案
您将浮点计算的结果存储在 int
数组中 - 这是一个错误,正如编译器警告的那样:
conversion from 'double' to 'int', possible loss of data
因此,9.2
的区域不会被视为小于 9.3
,因为它们都存储为 9
。
计算期间使用的数组应该是double
。所以这两个数组
int area[n];
int p[n];
应该是
double area[n];
double p[n];
关于HackerRank 网站中的 Clang 问题 "Small Triangles, Large Triangles",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58241073/
function birthdayCakeCandles(n, ar) { let max = 0; for(let i = 0; i max) { max = ar[i];
我正在尝试解决 https://www.hackerrank.com/challenges/find-hackerrank在 Obj-C 上,并通过 xCode 获得正常输出,但不是通过 hacker
我一直在努力解决这个挑战,并且有点想知道该怎么做。但是经过我所有的尝试,我只能通过测试用例,并且在提交面板中还有一个案例。之后一切都失败了 问题: 一家公司要求简化其产品分配策略,给定 n 个产品,每
问题陈述 :Sherlock and Moving Tiles TL;博士 : 给定 2 正方形;边长为 L,放置在 x-y 平面中,两个正方形 搬家沿线 y=x (沿正 x 和 y)速度为 S1 和
这个问题与this challenge有关在 HackerRank 上。它似乎在某些情况下失败了,但我不清楚算法有什么问题(很多人似乎有超时问题,这不是问题,一切都运行得很快,我看到的所有情况都通过了
我一直在应对这个挑战: Count Triplets ,经过大量的努力,我的算法并没有适用于每个测试用例。 由于在讨论中,我看到了一段代码并试图找出代码的真正功能,但我仍然无法理解这段代码是如何工作的
请参阅最近在 HackerRank 上发布的以下问题 Adam is standing at point (a,b) in an infinite 2D grid. He wants to know
(HakerRank) task是让我格式化测试用例给出的输入。 字符串和整数之间需要有 15 个空格,并且如果只有两位数,则在整数前面附加一个零,我的代码据我所知完成了这一点,并与预期的输出匹配,但
大多数人可能熟悉这个问题,但对于那些不熟悉的人来说: Given 32-bit unsigned integers flip their bits and print the resulting in
给定一个未排序的整数列表,找到它们之间绝对差最小的一对元素。如果有多对,请全部找到。 我的推理是将每个:arr[j] - arr[i] 与lowest 进行比较,如果它小于或等于该值,则将该值添加到数
我正在解决重复字符串 hackerrank 问题。但是所有的测试用例都没有运行可能是我的逻辑不正确。问题是这样的 下面给出了我的代码,其中包含没有变量的repeatedString和countingA
问题的链接如下: https://www.hackerrank.com/challenges/strange-code/problem static long strangeCounter(long
我正在尝试解决this hackerrank 上的问题,我花了一段时间才找到窍门。 技巧在于异或的属性以及数字出现在数组子集中的次数,其中子集是连续的(请注意)。 因此,如果我们有 1,2,3,子集将
我无法理解代码中这一行的用法,有人可以解释一下这个问题或提供一些不同的方法来解决这个问题 问题链接:https://www.hackerrank.com/challenges/15-days-of-l
为了问题陈述,我附上了两张照片。 /image/22zyM.png /image/5c8e6.png 我的代码: for(i=0;i #include #include #include int
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 6 年前。 Improve th
我试图解决this 。我的算法在离线编译器中给出了正确的答案。我不知道错误在哪里。我对 C 语言完全陌生。 该问题要求创建一个函数来查找方阵左右对角线的绝对差。 这是我的网站解决方案。当我在线运行代码
我想创建一个函数来返回一个数组,其中包含完成另一个数组所需的步骤数,但有一个小条件,我只想在 0 上执行步骤,这意味着如果我有数组 c = [0,0 ,0,1,0,0,1,0] 它将把前三个 0 作为
我正在尝试 hackerrank 中的 Morgan and a String 挑战( https://www.hackerrank.com/challenges/morgan-and-a-strin
我是初学者,欢迎替代此代码。输入值后程序崩溃。我也想更多地了解这个问题,因为我已经看过很多次了。 #include #include #include #include using namespace
我是一名优秀的程序员,十分优秀!