首页 > 解决方案 > 如何找到 10x12 矩阵的最小值和最大值

问题描述

我有一个使用 10x12 矩阵计算销售公司销售额的程序。

销售公司在 10 个城市设有分公司,程序必须打印每个分公司在过去 12 个月内的销售数量。然后打印当年的总销售额,那么最后就是打印销售额的最小值和最大值

但是程序没有打印正确的最小值和最大值

这是代码

using namespace std;
int main() {
    double F[10][12];
    double sum[10] = { 0 };
    double Msum[12] = { 0 };

    int i = 0;
    int j;
    double x = 0;
    double max = 0;
    double Minimum = 100000000;

    for (i = 0; i < 10; i++) {
        cout << "please enter the " << i + 1 << " branch sale for the " << endl;
        for (j = 0; j < 12; j++) {
            cout << "month " << j + 1 << ": " << endl;
            cin >> F[i][j];
        }
    }

    for (i = 0; i < 10; i++)
        for (j = 0; j < 12; j++)
            sum[i] += F[i][j];
    for (i = 0; i < 10; i++) {
        cout << "the total sales for branch " << i + 1 << " is " << sum[i] << endl;
    }

    for (i = 0; i < 10; i++)
        for (j = 0; j < 12; j++) {
            x += F[i][j];
        }
    cout << "the total sales for all branches in the year is " << x << endl;

    int min = INT_MAX;
    int max = INT_MIN; 

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j <= 3 / 2; j++)
        {
            if (F[i][j] > F[i][3 - j - 1])
            {
                if (min > F[i][3 - j - 1])
                    min = F[i][3 - j - 1];
                if (max < F[i][j])
                    max = F[i][j];
            }
            else
            {
                if (min > F[i][j])
                    min = F[i][j];
                if (max < F[i][3 - j - 1])
                    max = F[i][3 - j - 1];
            }
        }
    }


    cout << "the branch that has the maximum sale in the year is " << max << endl;
    cout << "the month that has the least sales in the year is " << min << endl;
    return 0;
}```

标签: c++

解决方案


推荐阅读