gpt4 book ai didi

c++ - 在这种情况下如何找到数组中最大值的位置?

转载 作者:行者123 更新时间:2023-11-28 04:55:34 25 4
gpt4 key购买 nike

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

class Triangle {
private:
double area;
double side1, side2, side3;

public:
Triangle() {
side1, side2, side3 = 0.0;
}
Triangle(double a, double b, double c) {
side1 = a, side2 = b, side3 = c;
}
void setsides(double a, double b, double c) {
side1 = a, side2 = b, side3 = c;
}
void calarea() {
double s = (side1 + side2 + side3) / 2;
area = sqrt(s*(s - side1)*(s - side2)*(s - side3));
}
double getarea() {
return area;
}

};

void main() {
double s1, s2, s3;
int n;
int i;
double max = 0;
int maxindex = 0;
Triangle arr[10];

cout << "Enter the number of triangles in range 1:10: ";
cin >> n;

for (i = 1; i < n + 1; i++) {
cout << "\nEnter the sides triangle " << i << ": \n";
cin >> s1 >> s2 >> s3;
arr[i].setsides(s1, s2, s3);
arr[i].calarea();
arr[i].getarea();
}

for (i = 1; i < n + 1; i++) {
cout << "Area of triangle " << i << " is: " << setprecision(2) << fixed << arr[i].getarea() << "\n";
}

for (i = 1; i < n + 1; i++) {
if (arr[i].getarea() > max) {
max = arr[i].getarea();
maxindex++;
}
cout << "\nThe largest area " << max << " is of triangle " << maxindex << endl;
}
}

我知道如何获取最大值,但不知道如何“不使用algorithmvector”获取最大值的位置。有一个规则,如果有两个相同的最大值,我们取前一个的位置。

例如,

Area of triangle 1 is: 1.98Area of triangle 2 is: 6.00Area of triangle 3 is: 6.00Area of triangle 4 is: 2.83The largest area 6.00 is of triangle 2

最佳答案

如何找到数组中的最大值。伪代码

max = 0
foreach element
if element > max then max = element

现在 max 有最大值。

关于c++ - 在这种情况下如何找到数组中最大值的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com