資源描述:
《【精品】算法分析代碼》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、符號(hào)三角形#ineludezziostream?h〃#include"math.h"classTriangle{friendintCompute(int);private:voidBacktrack(intt);intn,half,//n*(n+l)/4count,longsum;};voidTriangle::Backtrack(intt){if((count>half)
2、
3、(t*(tT)/2-count>half))return;if(t>n)sum++;elsefor(inti二0;i〈2;i++){p[l][t]二i;count+=i;for(intj二2;j〈t;j++){p
4、[j][t-j+l]二p[jT][t-j+l「p[j-l][t-j+2];count+二譏j][t-j+1];}Backtrack(t+1);for(j=2;j〈t;j++)count-=p[j][t-j+1];count-=i;}}intCompute(intn){TriangleX;X.n=n;X.count=0;X.sum二0;X.half=n*(n+l)/2;if(X.half%2=l)return0;X.half=X.half/2;int**p二newint*[n+l];for(inti=0;i5、or(intj=0;j〈n;j++)p[i][j]二0;X.p二p;X.Backtrack(1);returnX.sum;}voidmain(){inttmp;tmp二Compute(12);cout<#includcintstand(intn,intstart[],intend[],boolvale[]);voidsort(intn,intstart[],intend[]);voidmain()intn,out;intstart[100],cnd[100];boolvale[100]:f
6、streammyfilel,myfile2;myfilel?open("text1.txt〃,ios::in);myfilel>>n;cout<>start[i]>>cnd[i];}myfilel?close();sort(n,start,end);//調(diào)用函數(shù)非遞減排序for(i二0;i〈n;i++)vale[i]二0;out二stand(n,start,end,vale);//調(diào)用函數(shù)計(jì)算最小會(huì)場(chǎng)數(shù)myfile2?open("output?txt〃,ios::out);myfile2<7、?close();}intstand(intn,intstart[],intend[],boolva.le[]){intj二0,num二0;for(intk二0;k〈n;k++)if(valeLk]==0){for(inti=l;i=end[j]){vale[i]=l;vale[j]=l;??J=l;}num++;}returnnum;}//計(jì)算最小會(huì)場(chǎng)函數(shù)voidsort(intn,intstart[],intcnd[]){intstrangl,strang2;for(inti二0;i〈nT;i
8、++)for(intj二i+1;j〈n;j++){if(end[i]>end[j]){strangl二end[i];end[i]二end[j];end[j]=strangl;strang2=start[i];start[i]=start[j];start[j]=strang2;}//非遞減排序字典序問題#inelude#include#ineludeusingnamespacestd;intmain(){fstreaminfile,outfile;infile.open(z,input?txt氣ios::in);outfilc.op
9、cn("output?txt“,ios::out);/打開輸入文件cout<0){in