Problem z algorytmem plecakowym

0

#include <iostream>
#include <fstream>
 
using namespace std;
 
void Plecak(int p[], int w[], int n, int W){
int pom;
int S1[W+1][n+1]; //= new int[W+1][n+1];
int S2[W+1][n+1]; //= new int[W+1][n+1];
//generowanie tablic S1, S2
for(int i=0; i<=W; i++){S1[i][0]=0; S2[i][0]=0;}
for(int i=0;i<n+1;i++) {S1[0][i]=0; S2[0][i]=0;}
for(int i=1; i<=W; i++)
{
        for(int j=1; j<=n; j++)
        {
            S2[i][j]=0;
        }
}
for(int i=1; i<=W; i++)
{
        for(int j=1; j<=n; j++)
        {
            if(i-w[j-1]>=0)
            {
                pom=S1[i-w[j-1]][j-1]+p[j-1];
                if(pom<S1[i][j-1])
                {
                    S1[i][j]=S1[i][j-1]; /*S2[i][j]=0;*/
                }
                else
                {
                    S1[i][j]=pom;
                    S2[i][j]=1;
                }
            }
            else
            {
                S1[i][j]=S1[i][j-1]; /*S2[i][j]=0;*/
            }
        }
}
int maks=0,j1,pomo;
 
for(int i=1; i<=W; i++)
{
        for(int j=1; j<=n; j++)
        {
            if(S1[i][j]>maks)
            maks=S1[i][j];
        }
}
fstream plikk("Out.txt",ios::out);
 
for(int i=1; i<=W; i++)
{
        for(int j=1; j<=n; j++)
        {
            if(S1[i][j]==maks)
            {
                pomo=maks;
                j1=j;
                cout<<j1<<" ";
                plikk<<j1<<" ";
                while(pomo>0)
                {
                    pomo-=w[j1-1];
 
                    while(S1[pomo][j1]==S1[pomo][j1-1])
                        j1-=1;
 
                    if(j1==0) break;
                    cout<<j1<<" ";
                    plikk<<j1<<" ";
 
                }
                cout<<endl;
                plikk<<endl;
 
            }
        }
}
 
plikk.close();
cout<<endl;
 
for(int i=0;i<=W;i++)
{
    for(int j=0;j<=n;j++)
        {
            cout<<S1[i][j]<<" ";
        }
        cout<<endl;
}
cout<<endl;
for(int i=0;i<=W;i++)
{
    for(int j=0;j<=n;j++)
        {
            cout<<S2[i][j]<<" ";
        }
        cout<<endl;
}
 
 
 
}
int main()
{
    fstream plik("In.txt",ios::in);
    int n,W;
    plik>>n>>W;
    int p[n],w[n];
    for(int i=0;i<n;i++)
    {
        plik>>p[i]>>w[i];
    }
    plik.close();
    Plecak(p,w,n,W);
    fstream plikk("Out.txt",ios::out);
    plikk.close();
    return 0;
  }

Po wpisaniu w in.txt:
4 6
2 1
3 2
3 4
4 5

W pliku Out.txt nic się nie wyświetla, a ponadto w konsoli, wynik jest inny:

jest:
3 2

4 1

zamiast:
1 4

2 3

Co zrobiłem nie tak, że wynik jest ''odwrócony''? I dlaczego nie zapisuje wyniku do pliku?

1

I gdzie jest zapis do tego pliku?

    fstream plikk("Out.txt",ios::out);
    plikk.close();
2
  1. Dziel problem na małe funkcje
  2. Szczególnie oddzielaj operacje IO od obliczeń
  3. Jak używasz C++, to na całego. Nie mieszaj C do kodu C++ (int S1[W+1][n+1]; jest niezgodne ze standardem C++, ale kompilatory łykają, bo pozwalają mieszać C i C++).
  4. https://dsp.krzaq.cc/post/176/ucze-sie-cxx-kiedy-uzywac-new-i-delete
  5. uporządkuj wcięcia kodu.
struct Item {
    int price;
    int weight;
};
using Solutions = std::vector<std::vector<int>>;

Solutions solveBagProblem(int Capacity, std::vector<Items> items)
{
     ....
}

std::ostream& printSolution(std::ostream& out, const Solutions& solutions)
{
    for(auto& solution : solutions) {
        for(auto x: solution) {
            out << x << ' ';
        }
        out << '\n';
    }
    return out;
}

std::istream& loadData(std::istream& in, int& Capacity, std::vector<Items>& items)
{
   .....
    return in;
}

https://godbolt.org/z/79rb7cE84

1 użytkowników online, w tym zalogowanych: 0, gości: 1