程序博客网 > java执行多个cmd命令
来源:互联网 发布:java执行多个cmd命令 编辑:程序博客网 时间:2024/04/29 04:58
#include <iostream>#include <fstream>using namespace std;int j;template<class Type>class Loading{friend Type MaxLoading(Type [],Type,int,int [],int &maxcount);private:void Backtrack(int i);int n,* x, //当前解*bestx, //当前最优解 count;Type* l,c, //磁带长度cl, //当前占用磁带长度r, //剩余磁带长度 bestl; //最大利用磁带长度public: int maxcount; //存储程序数};template<class Type>void Loading<Type>::Backtrack(int i){if(i > n){if((count == maxcount && cl > bestl) || count > maxcount){for(j = 1; j <= n; j++){bestx[j] = x[j]; bestl = cl;}}}return;// 搜索子树r -= l[i];if(cl + l[i] <= c){x[i] = 1;cl += l[i];count++;Backtrack(i+1);cl -= l[i];count--;}if(cl + r > bestl){x[i] = 0;Backtrack(i+1);}r += l[i];}template<class Type>Type MaxLoading(Type l[], Type c, int n, int bestx[],int &maxcount){Loading<Type> X;X.x = new int [n+1];X.l = l;X.c = c;X.n = n;X.bestx = bestx;X.bestl = 0;X.cl = 0;X.r = 0;X.count = 0;for(int i = 1; i <= n; i++)X.r += l[i];X.Backtrack(1);maxcount = X.maxcount;delete []X.x;return X.bestl;}void main(){int n,c,i;int maxcount = 0;cin >> n >> c;int *l = new int [n+1];int *bestx = new int [n+1]; for(i = 1; i <= n; i++)cin >> l[i]; int bestl = MaxLoading(l,c,n,bestx); cout << maxcount << " " << bestl << endl;for(i = 1; i <= n; i++)if(bestx[i])cout << l[i] << " ";cout << endl;}/*void main(){ifstream infile("intput.txt");ofstream outfile("output.txt");int n,c,count,i;infile >> n >> c;int *l = new int [n+1];int *bestx = new int [n+1]; for(i = 1; i <= n; i++)infile >> l[i]; int bestl = MaxLoading(l,c,n,bestx,count);outfile << count << bestl << endl;for(i = 1; i <= n; i++)outfile << bestx[i];outfile << endl;infile.close();outfile.close();}