最小化c ++中的内存使用

问题描述 投票:0回答:1

我正在解决代码强制问题,代码功能没有问题,但是代码超出了内存使用量。有人可以解释如何改善这一点吗?编程时通常如何学习内存管理,因为我没有发现任何与此相关的东西。这是我的代码:

问题摘要:给您6个输入数字,前5个数字应分别乘以另一个整数,并且相乘后的总和为输入中的sixth整数。您应该找到可以与输入中的每个值相乘的所有数字组合,以求和。输出基本上是要乘以输入中每个数字所选择的整数的总和。

#include <iostream>
#include <vector> 

#ifndef MAX
#define MAX 100
#endif

using namespace std;

void storeAndFilter(vector <int> &arr,int chosenNumConst, int mypasha);
void print(vector<int> &v);
int printsum(vector<int> &v);


int main(int argc, char const *argv[])
{
    //array of input options
    int a1, a2, a3, a4, a5, pasha;
    cin >> a1 >> a2 >> a3 >> a4 >> a5 >> pasha;

    //declarations of vectors
    vector<int> arrStrA1;
    vector<int> arrStrA2;
    vector<int> arrStrA3;
    vector<int> arrStrA4;
    vector<int> arrStrA5;

    //sorting and filtering the vectors
    storeAndFilter(arrStrA1,a1,pasha);
    storeAndFilter(arrStrA2,a2,pasha);
    storeAndFilter(arrStrA3,a3,pasha);
    storeAndFilter(arrStrA4,a4,pasha);
    storeAndFilter(arrStrA5,a5,pasha);

    //cout<<"All Posibilities valid (Minimized by removing values < pasha) : "<<endl;
    // print (arrStrA1);
    // print (arrStrA2);
    // print (arrStrA3);
    // print (arrStrA4);
    // print (arrStrA5);

    //scores vectors
    vector<int> resultsA1;
    vector<int> resultsA2;
    vector<int> resultsA3;
    vector<int> resultsA4;
    vector<int> resultsA5;


    int i,j,k,l,m;
    for (i=0; i < (int)arrStrA1.size(); ++i)
    {
        for (j=0; j < (int)arrStrA2.size(); ++j)
        {
            for (k=0; k < (int)arrStrA3.size(); ++k)
            {
                for (l=0; l < (int)arrStrA4.size(); ++l)
                {
                    for (m=0; m < (int)arrStrA5.size(); ++m)
                    {
                        if(arrStrA1.at(i)+arrStrA2.at(j)+arrStrA3.at(k)+arrStrA4.at(l)+arrStrA5.at(m)==pasha)
                        { 

                            resultsA1.push_back(arrStrA1.at(i));
                            resultsA2.push_back(arrStrA2.at(j));
                            resultsA3.push_back(arrStrA3.at(k));
                            resultsA4.push_back(arrStrA4.at(l));
                            resultsA5.push_back(arrStrA5.at(m));


                        }

                    }   
                }   
            }   
        }
    }



    //divise each term by the card value
    for (int i = 0; i < (int)resultsA1.size(); ++i)
    {
        if (a1==0)
            resultsA1.at(i) /= 1;
        else
            resultsA1.at(i) /= a1;
    }
    for (int i = 0; i < (int)resultsA2.size(); ++i)
    {
        if (a2==0)
            resultsA2.at(i) /= 1;
        else
            resultsA2.at(i) /= a2;

    }
    for (int i = 0; i < (int)resultsA3.size(); ++i)
    {
        if(a3==0)
            resultsA3.at(i) /= 1;
        else
            resultsA3.at(i) /= a3;
    }
    for (int i = 0; i < (int)resultsA4.size(); ++i)
    {
        if (a4==0)
            resultsA4.at(i) /= 1;
        else        
            resultsA4.at(i) /= a4;
    }
    for (int i = 0; i < (int)resultsA5.size(); ++i)
    {
        if(a5==0)
            resultsA5.at(i) /= 1;
        else
            resultsA5.at(i) /= a5;
    }

    //Uncomment to show the table list after division
    // print(resultsA1);
    // print(resultsA2);
    // print(resultsA3);
    // print(resultsA4);
    // print(resultsA5);

    int scra1=printsum(resultsA1);
    int scra2=printsum(resultsA2);
    int scra3=printsum(resultsA3);
    int scra4=printsum(resultsA4);
    int scra5=printsum(resultsA5);

    cout << scra1 <<" "<< scra2 <<" "<< scra3 <<" "<<scra4 <<" "<< scra5 <<endl;


    return 0;
}


void print(vector<int> &v)
{
    int size = v.size();
    cout<<"========================"<<endl;
    for (int i = 0; i < size; ++i)
        cout<<v.at(i)<<endl;
    cout<<"========================"<<endl;

}

int printsum(vector<int> &v)
{
    int sum =0;
    for (int i = 0; i < (int)v.size(); ++i)
        sum += v.at(i);
    return sum;
}

void storeAndFilter(vector <int> &arr,int chosenNumConst, int mypasha)
{
    arr.reserve(10);
    int i=0;

    for (; i <= MAX; ++i)
    {
        arr.push_back(i*chosenNumConst);
        if (arr.at(i)>mypasha)  
            break;
    }

    arr.resize(i);
}

我想到的一些东西:

  • 用数组代替向量可能更好
  • 嵌套的for循环可能是占用过多内存的那个

但是要清楚,嵌套的for循环并没有进行太多的计算,他们发现5个数字的全部组合'5个循环'求和成一个特定值。在进入循环之前应用了过滤,因此嵌套循环可能不是问题。问题中的最大内存限制是:256 MB

c++ memory memory-management
1个回答
0
投票

通过不使用所有这些向量,您可以使用更少的内存。您可以这样编写代码:

    // make sure we handle zero properly
    auto end = [&](int num){
        return num == 0 ? num : pasha/num;
    };

    for (auto i=0, end_i = end(a1); i <= end_i; ++i)
    {
        for (auto j=0, end_j = end(a2); j <= end_j; ++j)
        {
            for (auto k=0, end_k = end(a3); k <= end_k; ++k)
            {
                for (auto l=0, end_l = end(a4); l <= end_l; ++l)
                {
                    for (auto m=0, end_m = end(a5); m <= end_m; ++m)
                    {
                        if(a1*i+a2*j+a3*k+a4*l+a5*m==pasha)
                        { 
                            std::cout << i << " " << j << " " << k << " " << l << " " << m << "\n";
                        }
                    }   
                }   
            }   
        }
    }

并且它输出所有有效结果。例如,输入1 2 3 4 5 6会产生

0 0 2 0 0
0 1 0 1 0
0 3 0 0 0
1 0 0 0 1
1 1 1 0 0
2 0 0 1 0
2 2 0 0 0
3 0 1 0 0
4 1 0 0 0
6 0 0 0 0

请参见工作示例here

© www.soinside.com 2019 - 2024. All rights reserved.