Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int N,K;//targyak szama, hatizsak kapacitasa
- int T[25],E[25];//tomegek es ertekek
- int H[25];// mit teszek a hatizsakba
- int sT[25];//a hatizsakban talalhato targyak tomege
- int sE[25];//a hatizsakban talalhato targyak erteke
- int db;
- int Optimalis;
- void Olvas()
- {
- ifstream f("taska.txt");
- f>>K;f>>N;
- for(int i=1;i<=N;i++)
- {
- f>>T[i]>>E[i];
- }
- f.close();
- }
- void Mutat()
- {
- if(sE[N]>=Optimalis)
- {
- Optimalis=sE[N];
- db++;
- cout <<db<<"."<<endl;
- cout <<"Index: ";
- for(int i=1;i<=N;i++)
- {
- cout.width(4);
- cout<<i<<" ";
- }
- cout <<endl;
- cout <<"Tomeg: ";
- for(int i=1;i<=N;i++)
- {
- cout.width(4);
- cout<<T[i]<<" ";
- }
- cout <<endl;
- cout <<"Ertek: ";
- for(int i=1;i<=N;i++)
- {
- cout.width(4);
- cout<<E[i]<<" ";
- }
- cout <<endl;
- cout <<"Zsak: ";
- for(int i=1;i<=N;i++)
- {
- cout.width(4);
- cout<<H[i]<<" ";
- }
- cout <<endl;
- cout <<"Ossztomeg: "<<sT[N]<<" Osszertek: "<<sE[N]<<endl;
- }
- }
- int Megoldas(int k)
- {
- return(k>N);
- }
- int Jo(int k)
- {
- return(sT[k]<K);
- }
- void Back(int k)
- {
- if(Megoldas(k))
- Mutat();
- else
- {
- for(int i=0;i<=1;i++)
- {
- H[k]=i;
- sT[k]=sT[k-1]+T[k]*i;
- sE[k]=sE[k-1]+E[k]*i;
- if(Jo(k))
- Back(k+1);
- }
- }
- }
- int main()
- {
- Olvas();
- Back(1);
- Mutat();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement