문제

The input should be n - the number of triangles (1 <= n <= 20) and afterwards n rows of three doubles each (corresponding to each of the triangles' sides). The output should be the "n" which has the maximum triangle area.

#include <iostream>
#include <math.h>
using namespace std;

const int MAX_SIZE = 20;

int main()
{
    int n, s, p;
    double max = 0;
    cin >> n;
    int x[MAX_SIZE];
    for (int i = 0; i < n; i++)
    {
        double y[2];
        for (int j = 0; j < 3; j++)
            cin >> y[j];
        p = (y[0] + y[1] + y[2]) / 2;
        s = sqrt(p * (p - y[0]) * (p - y[1]) * (p - y[3]));
        if (s >= max) max = s;
    }
    cout << max;
    return 0;
}

That's what I've done so far. "p" stands for semiparameter by the way.. - I'm using Heron's formula. I haven't even got it to "cout" the n in which the area is max but rather the maximum area itself, yet it doesn't work but gives me a massive error instead. Any ideas?

도움이 되었습니까?

해결책

You've got a few problems:

  • You need to change s and p from ints to doubles (otherwise you'll get unwanted truncation of your results).
  • You need to change double y[2]; to double y[3]; (since you need three side lengths, not two).
  • Change s = sqrt(p * (p - y[0]) * (p - y[1]) * (p - y[3])); to s = sqrt(p * (p - y[0]) * (p - y[1]) * (p - y[2])); (since y[3] is out of bounds of your array).

Note also that you can get rid of your array x, since you don't seem to actually use it anywhere.

다른 팁

You are allocation only 2 doubles. You need 3, try double y[3].

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top