我想要三个变量n1,n2,n3中两个最大变量的平均值。有人能帮我吗。

时间:2015-11-06 17:05:46

标签: c++

我想要三个变量n1,n2,n3中两个最大变量的平均值。有人能帮我吗。我要求用户输入三个音符将存储在变量n1,n2,n3中。那么我希望程序返回两个最大变量的平均值。

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    float ra[23], qte_alunos=0;
    float n1[29],n2[33],n3[33],op1[22],op2[22], fina[22];
    string nome[23], curso[23];

    for (int i=0; i<3; i++){

    cout << "digite RA: ";
    cin >> ra[i];
    cout << "digte nome: ";
    cin >> nome[i];
    cout << "digite curso: ";
    cin >> curso[i];
    cout << "digite N1: ";
    cin >> n1[i];
    cout << "digite N2: ";
    cin >> n2[i];
    cout << "digite N3: ";
    cin >> n3[i];

    if (n1[i] > n2[i] && n2[i] > n3[i]){
        n1[i] = op1[i];
        n2[i] = op2[i];
    }

    if (n2[i] > n3[i] && n3[i] > n1[i]){
        n2[i] = op1[i];
        n3[i] = op2[i];
    }

    if (n3[i] > n1[i] && n1[i] > n2[i]){
        n3[i] = op1[i];
        n1[i] = op2[i];
    }

    fina[i] = (op1[i]+op2[i])/2;

    if (fina[i] > 6 ){
        cout << "aprovado " << fina[i];
    }
    if (fina[i] > 4 && fina[i] < 5.9){
        cout << "exame " << fina[i];
    }

    if (fina[i] < 4){
        cout << "reprovado " << fina[i];
    }
    cout << "\n" << endl;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

如果我说得对你想要(sum(a, b, c) - min(a, b, c)) / 2

#include <algorithm>
#include <iostream>

int main (int argc, const char **argv) {
    double a = 1;
    double b = 2;
    double c = 3;
    double min = std::min({a, b, c});
    // double max = std::max({a, b, c});
    double sum = a + b + c;
    // double result = ((sum - min - max) + max) / 2;
    // which is:
    double result = (sum - min) / 2;
    std::cout << result << '\n';
}