程序——组合问题

 

/* * 解决任意长度字串中取任意个数N的组合情况 * 程序:一纯(CH.W) * 时间:2011/5/4 */ #define _CORE_CPP_ #ifdef _CORE_CPP_ #include <vector> #include <cstdio> #include <cstdarg> using namespace std; #define printf MyPrint #define _FILENAME_OF_RESULTS_ “result.txt” FILE* file; /* * 封装vprintf */ void MyPrint(const char* format, …){ va_list ap; va_start(ap,format); vprintf(format,ap); vfprintf_s(file,format,ap); va_end(ap); } /* * char[] 转 vector */ vector<char> _ctov(char _in[]){ vector<char> _out; for(unsigned int n = 0;_in[n]!=’/0′;n++){ _out.push_back(_in[n]); } return _out; } /* * 打印vector */ void PrintVector(vector<char> const & _in){ vector<char>::const_iterator cit = _in.begin(); printf(“{ “); while(cit != _in.end()){ printf(“%c “,*cit); cit++; } printf(“}”); } /* * 定义组合类 */ typedef class Combination{ public: /* * 默认构造1 */ Combination(); /* * 带参构造 */ Combination(char* src_in, int N_in, bool go); /* * 析构 */ ~Combination(); /* * 设置src */ int Set_src(char* _in); /* * 设置N */ int Set_N(int _in); /* * 得到res_n */ int Get_res_n(); /* * 启动计算 */ int Start(); private: vector<char> src; // 原字符串 int N; // 取得N个 int res_n; // 结果个数 /* * 迭代计算 */ int F_c(vector<char> src, vector<char>::iterator beginIt, vector<char>::iterator endIt, int getN, vector<char> dest); }Com,*pCom; /* * 构造函数 */ Combination::Combination(){ src.clear(); N = 0; res_n = 0; } /* * 带参构造 */ Combination::Combination(char* src_in, int N_in, bool go){ Combination(); Set_src(src_in); Set_N(N_in); if(go){ Start(); } return; } /* * 析构函数 */ Combination::~Combination(){ } /* * 设置src */ int Combination::Set_src(char* _in){ src.clear(); for(int n=0;n<(signed int)strlen(_in);n++){ src.push_back(_in[n]); } return -1; } /* * 设置N */ int Combination::Set_N(int _in){ N = _in; return N; } /* * 得到res_n */ int Combination::Get_res_n(){ return res_n; } /* * 启动计算 */ int Combination::Start(){ res_n = 0; // 清空总结果数 vector<char> dest; F_c(src,src.begin(),src.end(),N,dest); return -1; } /* * 迭代计算(递归) */ int Combination::F_c(vector<char> src, vector<char>::iterator beginIt, vector<char>::iterator endIt, int getN, vector<char> dest){ vector<char> sta_now(beginIt,endIt); if(getN <= (signed int)sta_now.size()){ if(getN == 0){ // 输出结果 res_n++; PrintVector(dest); if((res_n)%5 == 0){ printf(“/r/n”); } return -1; } dest.push_back(*beginIt); F_c(sta_now,beginIt+1,endIt,getN-1,dest); dest.pop_back(); F_c(sta_now,beginIt+1,endIt,getN,dest); } return -2; } /* * 驱动 */ #define _FUNCTION_3_ int main(){ #ifdef _FUNCTION_1_ char c[] = “abcdefghijklmn”; Com myC; myC.Set_src(c); myC.Set_N(3); myC.Start(); system(“pause”); #endif #ifdef _FUNCTION_2_ char c[] = “abcdefghijklmn”; Com myC(“abcdefghijklmn”,2,true); system(“pause”); #endif #ifdef _FUNCTION_3_ fopen_s(&file,_FILENAME_OF_RESULTS_,”w”); // 开文件 printf(“***************************/r/n”); printf(” Program: 一纯(CH.W)/r/n”); printf(” Function: 输出组合情况/r/n”); printf(“***************************/r/n”); char c[101]; int n; while(true){ printf(“Please input a STRING like /’abcde/’,/r/nand there would be construct a collection: { a b c d e }/r/nps:input /’exit/’ to quit/r/n>>”); scanf_s(“%s”,c,100); if(strcmp(c,”exit”) == 0){ goto _END_OF_THE_PROGRAM_; }else{ printf(“Data of a collection:”); PrintVector(_ctov(c)); printf(” have been created./r/n”); } _INPUT_N_: printf(“Please input a NUMBER like /’2/’./r/nIt means we will get how many element(s) from that collection above./r/n>>”); scanf_s(“%d”,&n); if(n > (signed int)strlen(c)){ printf(“The number of your input is too BIG!/r/nWe need a smaller one./r/n”); goto _INPUT_N_; }else if(n < 1){ printf(“It is no means to input a negative number or zero./r/n”); goto _INPUT_N_; }else{ printf(“Data of N has been set.(N=%d)/r/n”,n); } printf(“–Result:—————————–/r/n”); Com myC(c,n,true); printf(“/r/nTotal:%d/r/n—————————————/r/n/r/n”,myC.Get_res_n()); } fclose(file); // 关文件 #endif _END_OF_THE_PROGRAM_: return -1; } #endif 

点赞